-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?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
None
  • 5,582
  • 21
  • 85
  • 170
  • 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);` – Tim Schmelter Sep 14 '17 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. – Souvik Ghosh Sep 14 '17 at 11:33
  • if you post the way in which you display the date we can tell you what the problem ist – Mong Zhu Sep 14 '17 at 11:33
  • Possible duplicate of [C# DateTime to "YYYYMMDDHHMMSS" format](https://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) – EpicKip Sep 14 '17 at 11:34
  • 14 month doesn't exist so your output has to be `MM/dd/yyyy` – fubo Sep 14 '17 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. – DiskJunky Sep 14 '17 at 11:50
  • @DiskJunky I never talked about how its stored. Im talking about how it appears while debugging. – Sujit.Warrier Sep 14 '17 at 12:00
  • @Sujit.Warrier, fair point. In debugging, it'll use the development environment's regional settings – DiskJunky Sep 14 '17 at 12:01
  • yes fair point. not always. but I think It does appear to the OP. – Sujit.Warrier Sep 14 '17 at 12:05

3 Answers3

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);
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 2
    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: https://stackoverflow.com/questions/13725575/getting-wrong-datetime-format – Tim Schmelter Sep 14 '17 at 11:51
  • @TimSchmelter thank you for pointing out. I incorporated it into my answer – Mong Zhu Sep 14 '17 at 11:58
  • @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 Sep 14 '17 at 12:01
  • @DiskJunky: i don't see that he actually uses `/`, otherwise there was no need to specify `CultureInfo.InvariantCulture` what he did. – Tim Schmelter Sep 14 '17 at 12:02
  • 1
    @DiskJunky: checked it, his city is Kerala so the culture is `ml-IN` and it indeed uses `/` ;-) – Tim Schmelter Sep 14 '17 at 12:07
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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
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").

DiskJunky
  • 4,750
  • 3
  • 37
  • 66