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

10
  • 2
    You are giving it an explicit format and then expecting it to use the regional one? Commented May 6, 2014 at 16:11
  • Yeah. Looks like epic programmer fail. You do NOT call "ToString()", you call an overload that says exactly how you want this one formatted. Commented May 6, 2014 at 16:12
  • What format do you want it in? Commented May 6, 2014 at 16:12
  • No, I am expecting to get the format that is specified as parameter to ToString method. Showing the regional format just in case they are having any effect on the output. Commented May 6, 2014 at 16:13
  • 1
    @Brij I am simply trying to make your question clearer, the screenshot is very small and truth be told, not entirely needed. You could have shown expected output versus actual and it would have been clearer. Commented May 6, 2014 at 16:15

3 Answers 3

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) 
Sign up to request clarification or add additional context in comments.

4 Comments

and we gotta winner.. +1
@Habib, why did the / turn into -. Why are they being changed internally, despite being specified explicitly ?
@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: stackoverflow.com/questions/2329297/…
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().

Comments

0

You can use this:

 DateTime.now.ToString("yyyyMMddHHmmss"); 

or

 DateTime.now.ToString("mm-dd-yyyy"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.