gpt4 book ai didi

.NET 时区信息

转载 作者:行者123 更新时间:2023-12-02 02:33:31 28 4
gpt4 key购买 nike

C# ASP.NET 应用程序中带时区的日期时间的标准格式是什么?

已更新以澄清:在 ASP.NET 中,我需要检查 Domino 中的日期时间.NET DateTime 的数据库。客户问他应该提供带时区的日期时间的最佳格式是什么。意思是哪种格式最容易被 .NET 读取?

最佳答案

更新 - 关于您的澄清:

I need to check datetime from a dominos database against asp.net datetime. The client is asking what would be the best format in which he should provide the datetime with the timezone.-meaning which is easily readable by .Net.

.NET 支持解析多种不同的格式,因此只要它是相当标准的明确格式,您就可以毫无问题地解析它。但是不要依靠默认设置让它碰运气,使用 ParseExactTryParseExact .

查看以下资源以获取有关构造格式字符串的详细信息:

如果您想向他提出建议,那么我建议您关注 ISO 8601 standard ,看起来像这样:2010-05-14T10:04ZDateTime.Parse也可以在不需要提供格式字符串的情况下理解这种格式。


回答你原来的问题:

What is the standard format for DateTime with timezone in a C# ASP.NET application?

DateTimes 的默认格式基于当前语言环境。如果您想要一致的格式,请使用 CultureInfo.InvariantCulture或者您选择的特定文化。

DateTime now = DateTime.Now;
string s = now.ToString(CultureInfo.InvariantCulture);

结果:

"05/14/2010 21:17:35"

请注意,默认格式不包括时区。不过,您可以指定自己的格式:

string s = now.ToString("yyyy-MM-dd HH:mm:ss zz");

结果:

"2010-05-14 21:21:59 +02"

关于.NET 时区信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2836896/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com