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

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -