设置默认日期时间格式 c#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1389187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Default DateTime Format c#
提问by Matthew Hood
Is there a way of setting or overriding the default DateTime format for an entire application. I am writing an app in C# .Net MVC 1.0 and use alot of generics and reflection. Would be much simpler if I could override the default DateTime.ToString() format to be "dd-MMM-yyyy". I do not want this format to change when the site is run on a different machine.
有没有办法为整个应用程序设置或覆盖默认的 DateTime 格式。我正在用 C# .Net MVC 1.0 编写一个应用程序,并使用了很多泛型和反射。如果我可以将默认的 DateTime.ToString() 格式覆盖为“dd-MMM-yyyy”,会简单得多。当站点在不同的机器上运行时,我不希望这种格式发生变化。
Edit - Just to clarify I mean specifically calling the ToString, not some other extension function, this is because of the reflection / generated code. Would be easier to just change the ToString output.
编辑 - 只是为了澄清我的意思是专门调用 ToString,而不是其他一些扩展函数,这是因为反射/生成的代码。仅更改 ToString 输出会更容易。
采纳答案by Paolo Tedesco
The "default format" of a datetime is:
日期时间的“默认格式”是:
ShortDatePattern + ' ' + LongTimePattern
at least in the current mono implementation. This is particularly painful in case you want to display something like 2001-02-03T04:05:06Z i.e. the date and time combined as specified in ISO 8606, but not a big problem in your case:
至少在当前的单声道实现中。如果您想显示类似 2001-02-03T04:05:06Z 的内容,即ISO 8606 中指定的日期和时间组合,这尤其痛苦,但在您的情况下不是大问题:
using System;
using System.Globalization;
using System.Threading;
namespace test {
public static class Program {
public static void Main() {
CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
culture.DateTimeFormat.LongTimePattern = "";
Thread.CurrentThread.CurrentCulture = culture;
Console.WriteLine(DateTime.Now);
}
}
}
This will set the default behavior of ToString on datetimes to return the format you expect.
这将在日期时间设置 ToString 的默认行为以返回您期望的格式。
回答by ema
You can write an ExtensionMethod like this:
你可以像这样写一个 ExtensionMethod :
public static string ToMyString(this DateTime dateTime)
{
return dateTime.ToString("needed format");
}
回答by Seb Nilsson
It is dependent on your application's localization-settings. Change that accordinglyto get correct format.
它取决于您的应用程序的本地化设置。相应地更改它以获得正确的格式。
Otherwise have a helper-class or an extension-method which always handles your DateTime.
否则有一个帮助类或扩展方法总是处理你的日期时间。
public static string ToMyDateTime(this DateTime dateTime) {
return dateTime.ToString("dd-MMMM-yy");
}
回答by Tadas ?ukys
DateTime.ToString()combines the custom format strings returned by the ShortDatePattern and LongTimePattern properties of the DateTimeFormatInfo. You can specify these patterns in DateTimeFormatInfo.CurrentInfo.
则DateTime.ToString()组合由ShortDatePattern和的LongTimePattern属性返回的自定义格式字符串的DateTimeFormatInfo。您可以在DateTimeFormatInfo.CurrentInfo 中指定这些模式。
I've never tried this my self.
我自己从来没有试过这个。
回答by Fernando
回答by Bjorn Bailleul
If you want to be sure that your culture stays the same, just set it yourself to avoid troubles.
如果您想确保您的文化保持不变,只需自己设置即可避免麻烦。
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("nl-BE");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
The above example sets the culture of the thread to Belgian-Dutch.
上面的示例将线程的文化设置为比利时-荷兰语。
CurrentCulture does all the date and time handling and CurrentUICulture handles UI localization like resources.
CurrentCulture 执行所有日期和时间处理,CurrentUICulture 处理 UI 本地化(如资源)。