asp.net – date format convertion – Education Career Blog

i have date like

string date = calarc.SelectedDate.ToString("MM/dd/yyyy");

where calarc is calender control.

It returns a date in 03-14-2009 format but i want it in 03/14/2009 format.

so how do i do above?

,

I think your current culture is messing with format. Try

DateTime.Parse("2009-03-14").ToString(@"MM/dd/yyyy", CultureInfo.InvariantCulture)

As it seems “/” is realy a syntax for separator (similar to M, d, y or any other). That’s why it is being replaced with separator specified in culture. This is noted in documentation too.

If the custom pattern includes the
format pattern “/”, DateTime.ToString
displays the value of DateSeparator in
place of the “/” in the format
pattern.

,

The / character in the format specifier doesn’t do what you think. It’s not a literal character that is copied to the string, instead it’s a specifier for the date separator, which in the current culture is the – character.

To use literal characters in the format just put apostrophes around them:

string date = calarc.SelectedDate.ToString("MM'/'dd'/'yyyy");

,

Untested line of code –

string.Format("mm-dd-yyyy", calarc.SelectedDate);

,

string.Format("{0:MM/dd/yyyy}",calarc.SelectedDate);

Leave a Comment