Saturday, August 4, 2012

DateTime.ParseExact - culturewise Date Comparision


Whenever dealing with cultures, DateTime format is always a headache to handle.

to the date format  in Textbox sometimes does not meet the format of culture.
in that case DateTime.ParseExact is good option to handle.

convert the date into current culture format from display culture format using

public static DateTime ParseExact( string str, string Dateformat, IFormatProvider provider)


Type: System.String
A string that contains a date and time to convert.
Type: System.String
A format specifier that defines the required format of s.
Type: System.IFormatProvider
An object that supplies culture-specific format information about s.
DateTime sessionDate = DateTime.ParseExact(txtDate.Text, "M-d-yyyy", provider);
here provide is
CultureInfo provider = CultureInfo.InvariantCulture;
this will get txtDate.Text (Date) into culture wise date format to compare with database formatted datetime.

Hope this Helps

All the best.