2017-09-19 59 views
-2
没有得到
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern="DD/MM/YYYY"; 
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern ="hh:mm tt"; 

我重写的日期和时间格式为一个文化线程通过这样做,我们将得到DateTime在给定的格式在DateTime.Now更新文化很短的时间模式格式DateTime.now

我能够得到首选格式为Date同样的事情不适用于时间。

如何使用上述文化线程获取首选格式的时间。

+0

,什么是你的问题你的参考? – Backs

+0

你到底在问什么? – Valkyrie

+0

欢迎来到堆栈溢出。你可以请创建一个[MCVE]展示你的问题?没有它就很难理解。还有,阅读[FAQ]和[问]几次会更好。 –

回答

0

您可能需要创建一个类型为“System.Globalization.CultureInfo”的对象,并在该对象上设置日期和时间格式规范。

接下来,您需要将线索的当前文化设置为该文化。

我给的代码下面

private void UpdateCurrentCulture() 
    { 
     System.Globalization.CultureInfo objCulture = new System.Globalization.CultureInfo("en-US"); 

     objCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"; 
     objCulture.DateTimeFormat.ShortTimePattern = "hh:mm tt"; 

     System.Threading.Thread.CurrentThread.CurrentCulture = objCulture; 
     System.Threading.Thread.CurrentThread.CurrentUICulture = objCulture; 

     Console.WriteLine(DateTime.Now.ToShortDateString()); 
     Console.WriteLine(DateTime.Now.ToShortTimeString()); 
    } 
+0

非常感谢你 –

+0

@ bharathp.v随时伴侣! – Dinny