.

Saturday, June 14, 2008

Convert Date from any format to any format

Convert Date from any format to any format

I come accross a code snippet while development which can be useful to convert the date display format.

//For ex., Convert MM/dd/YYYY to dd/MM/yyyy

string date = "03/27/2008"; //format is MM/dd/yyyy

DateTimeFormatInfo dateTimeFormatterProvider = DateTimeFormatInfo.CurrentInfo.Clone() as DateTimeFormatInfo;

dateTimeFormatterProvider.ShortDatePattern = "MM/dd/yyyy"; //source date format

DateTime dateTime = DateTime.Parse(date, dateTimeFormatterProvider);

string formatted = dateTime.ToString("dd/MM/yyyy"); //write the format in which you want the date tobe converted

Response.Write("
"
+ formatted);

No comments:

.