简体   繁体   English

如何在C#中获取当前的短日期时间格式?

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

How do I get the sort DateTime format of the local system, in string format, using C#? 如何使用C#以字符串格式获取本地系统的DateTime格式?

For example: dd/MM/yyyy or MM/dd/YYYY 例如: dd/MM/yyyyMM/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 :-) 我不是直接使用Thread.CurrentThread.CurrentCulture因为有人可能搞乱了它:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM