Rocky Singh
Rocky Singh

Reputation: 15440

Date separator issue

I have the following code

DateTime.Now.ToString("MM/dd/yyyy")

It always gives me this output : "04.13.2011" instead of "04/13/2011". May I know why I am getting this weird issue?

Upvotes: 13

Views: 8198

Answers (3)

Chirag
Chirag

Reputation: 4184

Use following code:

DateTime.Now.ToString("MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)

This ensures that the underlying date and time values do not change when the data is read or written by users from different cultures.

Upvotes: 1

Rikin Patel
Rikin Patel

Reputation: 9383

Try this

DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1502616

You're almost certainly in a culture where that's the default date separator. If you want to force / you can quote it in the format string:

string x = DateTime.Now.ToString("MM'/'dd'/'yyyy")

Upvotes: 26

Related Questions