Set Default DateTime Format c#(设置默认日期时间格式 c#)
问题描述
有没有办法为整个应用程序设置或覆盖默认的 DateTime 格式.我正在用 C# .Net MVC 1.0 编写一个应用程序,并使用了很多泛型和反射.如果我可以将默认的 DateTime.ToString() 格式覆盖为dd-MMM-yyyy",将会简单得多.当网站在不同的机器上运行时,我不希望这种格式发生变化.
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.
编辑 -只是为了澄清我的意思是专门调用 ToString,而不是其他扩展函数,这是因为反射/生成的代码.只更改 ToString 输出会更容易.
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.
推荐答案
日期时间的默认格式"是:
The "default format" of a datetime is:
ShortDatePattern + ' ' + LongTimePattern
至少在当前 mono实施.如果您想显示类似 2001-02-03T04:05:06Z 的内容,即 ISO 8606,但在您的情况下不是大问题:
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:
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);
}
}
}
这将设置 ToString 在日期时间上的默认行为以返回您期望的格式.
This will set the default behavior of ToString on datetimes to return the format you expect.
这篇关于设置默认日期时间格式 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:设置默认日期时间格式 c#
基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
