简体   繁体   English

集中设置dateTime.ToString()格式

[英]Setting dateTime.ToString() format centrally

如何集中设置DateTime格式,以便在任何时候如果我在代码中使用DateTime.ToString() ,我将获得ISO格式的字符串(例如: 2008-2-19 01:00:00 )或( 2008-2-19

You should use CultureInfo to control the format when using DateTime.ToString() 使用DateTime.ToString()时应使用CultureInfo控制格式

Once you have set the CurrentCulture on your Current Thread then try the following: 在Current Thread上设置CurrentCulture之后,请尝试以下操作:

DateTimeFormatInfo format = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
string dateTime = DateTime.Now.ToString(format.FullDateTimePattern);

You can add the time format as parameter to the tostring, i always use this for reference 您可以将时间格式作为参数添加到tostring中,我总是将其用作参考

Dim d = DateTime.Parse("2008-2-19 01:00:00")
Assert.AreEqual("2008-2-19 01:00:00", d.ToString("yyyy-M-dd HH:mm:ss"))

http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

Regards 问候

Iain 伊恩

You should update the System.Threading.Thread.CurrentThread.CurrentCulture property. 您应该更新System.Threading.Thread.CurrentThread.CurrentCulture属性。

This affects all DateTime.ToString() in the current thread. 这会影响当前线程中的所有DateTime.ToString()

With the help of Albin and Barry's answers iv got the following piece to code to set the Time format centrally in the Global.asax. 在Albin和Barry的答案的帮助下,我得到了以下代码,以便在Global.asax中集中设置时间格式。

using System.Globalization;
using System.Threading;

protected void Application_BeginRequest()
        {
            CultureInfo standardizedCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            standardizedCulture.DateTimeFormat.DateSeparator = "-";
            standardizedCulture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd hh:mm:ss";
            standardizedCulture.DateTimeFormat.FullDateTimePattern = "yyyy-MM-dd hh:mm:ss";           
            standardizedCulture.DateTimeFormat.ShortDatePattern  = "yyyy-MM-dd"; 
            Thread.CurrentThread.CurrentCulture = standardizedCulture;
            Thread.CurrentThread.CurrentUICulture = standardizedCulture;
        } 

You can use extension methods to extend datetime and create a ToISOString(). 您可以使用扩展方法来扩展日期时间并创建ToISOString()。

check out http://msdn.microsoft.com/en-us/library/bb383977.aspx on ways to accomplish it. 查看http://msdn.microsoft.com/en-us/library/bb383977.aspx了解如何完成它。 In the extension method you can use parameters to format the string the way you need, then you can use DateTime.ToISOString(); 在扩展方法中,您可以使用参数以您需要的方式格式化字符串,然后您可以使用DateTime.ToISOString(); You could also use cultureinfo as Barry said, but I don't know if it will fit your needs. Barry说,您也可以使用cultureinfo,但我不知道它是否符合您的需求。

I think the requested format ie DateTIme.Now.ToString() will have a very bad influence on your code readability (and maintainability..). 我认为请求的格式,即DateTIme.Now.ToString()将对您的代码可读性(和可维护性......)产生非常不利的影响。 Trying to override a well known behavior with a custom one is bad practice. 试图用自定义行为覆盖众所周知的行为是不好的做法。
What I do consider a good way to use it is like so: DateTIme.Now.ToString(IsDefaultFormat) . 考虑使用它像这样的好方法是什么: DateTIme.Now.ToString(IsDefaultFormat)
Now all you need to do is add an extension method to DateTime Which receives a bool , and if that bool is set to true, returns the DateTime using your "default format" 现在您需要做的就是向DateTime添加一个扩展方法,它接收一个bool ,如果该bool设置为true,则使用您的“默认格式”返回DateTime

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

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