开发者

How Can I Get the Current Short DateTime Format in C#?

How do I get the sor开发者_开发技巧t DateTime format of the local system, in string format, using C#?

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


Try this:

using System.Globalization;

...
...
...

var myDTFI = CultureInfo.CurrentCulture.DateTimeFormat;

var result = myDTFI.FullDateTimePattern;


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 :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜