c# - Compare DateTime and date as string -


i have value of datetime , date string. string date can in unknown format (1 of 2 in case : "mmm dd yyyy" or "dd mmm" ). need check if dates eaqual.

is there other solution trying parse string date first , second formats , if 1 return datetime compare datetime type?

of course. can use datetime.parseexact method. there several overloads of function.

one of overloads parseexact(string, string[], iformatprovider, datetimestyles).

converts specified string representation of date , time datetime equivalent using specified array of formats, culture-specific format information, , style. format of string representation must match @ least 1 of specified formats or exception thrown.

string[] formats= {"mmm dd yyyy", "dd mmm"};  var datevalue = datetime.parseexact(datestring,                    formats,                     cultureinfo.invariantculture,                    datetimestyles.none); 

keep in mind that, format of string representation must match @ least 1 of specified formats or exception thrown. if didn't want use try/catch block explicitly, best choice tryparseexact method. return true if parameter converted successfully; otherwise, false.

datetime datevalue;  nullable<datetime> result = datetime.tryparseexact(datestring, formats,            cultureinfo.invariantculture,           datetimestyles.none,            out datevalue)) ?      datevalue :     (datetime?)null; 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -