-1

My code is like this

 DateTime dt = new DateTime(); dt= DateTime.ParseExact("14/09/2017", "dd/MM/yyyy", CultureInfo.InvariantCulture); 

I am expecting dt to have a format of dd/MM/yyyy but the output I am getting is in MM/dd/yyyy format. This is the correct out put I am getting 9/14/2017 12:00:00 AM. Can anyone please point out what I am doing wrong here?

9
  • 3
    A datetime has no format, question can be closed. If you want to convert it again to string use dt.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture); Commented Sep 14, 2017 at 11:33
  • Can't you use- dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); As @TimSchmelter mentioned, Datetime object has no format. You decide the format only when displaying it somewhere. Commented Sep 14, 2017 at 11:33
  • if you post the way in which you display the date we can tell you what the problem ist Commented Sep 14, 2017 at 11:33
  • Possible duplicate of C# DateTime to "YYYYMMDDHHMMSS" format Commented Sep 14, 2017 at 11:34
  • 1
    @Sujit.Warrier DateTime is stored as a 8 byte binary block, not as a MM/dd/yyyy string. It's only on a call to the object's ToString() method that the current culture's format is applied and the text generated. Commented Sep 14, 2017 at 11:50

3 Answers 3

2

if you expect the format "dd/MM/yyyy" you need to specify it when displaying the DateTime. To do so you can use this overload of the ToString method:

dt.ToString("dd/MM/yyyy"); 

A DateTime on it's own has no format. Only the string representation of it has one.

EDIT:

Important remark by Tim Schmelter:

/ is a custom format specifier which replaces all occurences with the local date-separator. You either have to esacape them by embedding them within ' or use CultureInfo.InvariantCulture as second parameter. Read this post

That means either use this:

string str_rep = dt.ToString("dd'/'MM'/'yyyy"); 

or:

string str_rep = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); 
Sign up to request clarification or add additional context in comments.

5 Comments

This is not the same, / is a custom format specifier which replaces all occurences with the local date-separator. You either have to esacape them by embedding them within ' or use CultureInfo.InvariantCulture as second parameter. Read: stackoverflow.com/questions/13725575/…
@TimSchmelter thank you for pointing out. I incorporated it into my answer
@TimSchmelter, good technical detail on the forward slashes but unlikely to cause an issue for the OP as they seem to be operating in an environment/culture where the format code "/" is actually the "/" itself. Good to be aware though
@DiskJunky: i don't see that he actually uses /, otherwise there was no need to specify CultureInfo.InvariantCulture what he did.
@DiskJunky: checked it, his city is Kerala so the culture is ml-IN and it indeed uses / ;-)
1

Your dt is a DateTime, not a string. Format concept only applies when you get their textual (aka string) representation. What you saw is probably what debbuger/ide shows you as a textual representation.

If you get a specific format of your dt, then you can use .ToString() method with dd/MM/yyyy format and a proper culture like InvariantCulture.

string myFormat = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); 

For beginners, it is really important to understand the difference between what is a DateTime and what is their string representation.

Comments

0

If DateTime.ToString() is resulting in an unexpected format, the likely case is that the current culture isn't being set. By default, the date format used is taken from the host machine. However, you can either specify it directly by setting the thread's CurrentCulture for the culture code you need, or you can set it in your application's configuration file. E.g., a web application's web.config can have a globalization section, like so;

<globalization culture="en-GB" uiCulture="en-GB" /> 

Alternatively, as already specified, you can set the format explicitly via a custom format string .ToString("dd/MM/yyyy").

1 Comment

Better ToString("dd'/'MM'/'yyyy") if you want to see the slashes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.