7

How do I get the sort DateTime format of the local system, in string format, using C#?

For example: dd/MM/yyyy or MM/dd/YYYY

Jonathan Dickinson
  • 9,050
  • 1
  • 37
  • 60
GSReddy
  • 137
  • 1
  • 5
  • 13
  • 3
    why do you want to know why he wants to know ? :D – Davide Piras Sep 07 '11 at 09:08
  • @Davide Because new questioners 2 out of 3 times don't know what they want? Perhaps he simply needs `CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern`? – xanatos Sep 07 '11 at 09:15

2 Answers2

9

Try this:

using System.Globalization;

...
...
...

var myDTFI = CultureInfo.CurrentCulture.DateTimeFormat;

var result = myDTFI.FullDateTimePattern;
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
2

Overkill but should do it:

CultureInfo info = null;

var thread = new Thread(() => {
    info = Thread.CurrentThread.CurrentCulture;
    return;
});

thread.Start();
thread.Join();

// info.DateTimeFormat.ShortDatePattern

Based on the fact that new threads should inherit the standard culture of the system (I consider it a missing feature: it's nearly impossible to control the culture of new threads, read here Is there a way of setting culture for a whole application? All current threads and new threads?). I'm not directly using Thread.CurrentThread.CurrentCulture because someone could have messed with it :-)

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280