Vikas
Vikas

Reputation: 24342

date format convertion

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

Answers (4)

Guffa
Guffa

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

Sergej Andrejev
Sergej Andrejev

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

Martin Clarke
Martin Clarke

Reputation: 5627

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

Upvotes: 0

Kunal S
Kunal S

Reputation: 107

Untested line of code -

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

Upvotes: 0

Related Questions