0

In the screenshot DateTime.ToString() method is being called but the date is not getting formatted in expected format (as seen in Quick Watch widnow). Is something wrong ?

enter image description here

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Brij
  • 11,731
  • 22
  • 78
  • 116

3 Answers3

4

You are using / as separator in your ToString format. But your current culture seems to has - as date separator. That is why you see the difference. You can pass CultureInfo.InvariantCulture with ToString.

Like:

DateTimeObject.ToString("MM/dd/yyy HHmmss", CultureInfo.InvariantCulture)
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    and we gotta winner.. +1 – Sriram Sakthivel May 06 '14 at 16:13
  • @Habib, why did the `/` turn into `-`. Why are they being changed internally, despite being specified explicitly ? – Brij May 06 '14 at 16:21
  • @Brij [Here is the documentation](http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx#dateSeparator) – Sriram Sakthivel May 06 '14 at 16:22
  • @Brij, Sriram, has pointed to the right documentation, but just to add a one more thing, When you don't specify the culture in `ToString` then the current culture is used. You may see: http://stackoverflow.com/questions/2329297/net-are-there-any-differences-between-invariantculture-and-en-us – Habib May 06 '14 at 16:27
0

DateTime.ToString replaces / with the current date separator and : with the current time separator. You're passing in the format yourself, and it does not match what's in the Region settings.

To use the Region settings, use ToShortDateString() and ToShortTimeString().

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

You can use this:

   DateTime.now.ToString("yyyyMMddHHmmss");

or

   DateTime.now.ToString("mm-dd-yyyy");
Ricardo Romo
  • 1,588
  • 12
  • 25