如何获取System DateTime格式?

Bre*_*ead 32 .net c# datetime

我正在寻找一个获得系统日期时间格式的解决方案.

例如:如果我得到了DateTime.Now?使用哪个日期时间格式?DD/MM/YYYY等等

Ode*_*ded 49

如果它没有在其他地方更改,这将得到它:

string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
Run Code Online (Sandbox Code Playgroud)

如果使用WinForms应用程序,您还可以查看UICulture:

string sysUIFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;
Run Code Online (Sandbox Code Playgroud)

请注意,这DateTimeFormat是一个读写属性,因此可以更改.

  • 调用`CultureInfo.CurrentCulture.DateTimeFormat;`不返回字符串值.最后的调用应该是`string sysDateFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;`以防你正在寻找系统短日期格式. (3认同)

RcM*_*Man 17

上面的答案并不完全正确.

我有一种情况,我的主线程和我的UI线程被迫处于"en-US"文化(按设计).我的Windows DateTime格式为"dd/MM/yyyy"

string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
string sysUIFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;
Run Code Online (Sandbox Code Playgroud)

返回"MM/dd/yyyy",但我想获得真正的Windows格式.我能够做到的唯一方法是创建一个虚拟线程.

System.Threading.Thread threadForCulture = new System.Threading.Thread(delegate(){} );
string format = threadForCulture.CurrentCulture.DateTimeFormat.ShortDatePattern;
Run Code Online (Sandbox Code Playgroud)