Reputation: 24342
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?
Upvotes: 2
Views: 288
Reputation: 700910
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");
Upvotes: 1
Reputation: 9423
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.
Upvotes: 4
Reputation: 107
Untested line of code -
string.Format("mm-dd-yyyy", calarc.SelectedDate);
Upvotes: 0