- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 ParseExact 方法的第一次重载中
public static DateTime ParseExact (string s, string format, IFormatProvider provider);
根据微软的说法:
If format is a custom format pattern that does not include date or time separators (such as "yyyyMMddHHmm"), use the invariant culture for the provider parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H".
特别是如果我们使用标准格式模式,我们可以使用任何其他文化
如果我们使用不包含日期或时间分隔符的自定义格式模式,那么使用不变文化和最宽自定义说明符的真正目的是什么?
最佳答案
InvariantCulture
的目的是采用一种不依赖于系统或用户区域设置的众所周知的方式来格式化日期和数字。
每次格式化不打算由人类解析的内容时,您都应该使用它。例如,在 JSON 或 XML 文件中,您希望以 ISO 格式存储日期,这样就不会出现歧义。另一方面,如果您在屏幕上显示日期,您通常会尊重用户的文化选择并以首选方式显示。
如果您使用自定义格式,那么如果可以将生成的字符串解析回完全相同的日期和时间,那肯定会很好。为了在没有分隔符的情况下做到这一点,您必须为每个组件使用固定长度的字符串。
// omitting CultureInfo.InvariantCulture for brevity
var dt = new DateTime(2018,1,2,3,45,6);
dt.ToString("yyyyMMddHHmmss") // returns "20180102034506"
dt.ToString("yyyyMdHms") // returns "2018123456"
您可以很容易地看出第二个不是唯一的,即还有其他日期将返回相同的字符串。为了避免您使用每个组件的更宽形式。
关于c# - 在 DateTime.ParseExact 中将不变文化与 IFormatProvider 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51820648/
我正在尝试将字符串变量转换为日期时间格式: [DateTime]::ParseExact($tempdate, 'dd.MM.yyyy', [CultureInfo]::InvariantCultur
这个 DateTime EndTime = DateTime.ParseExact("24/07/2015 12:00 AM", "DD/MM/YYYY hh:mm tt", new CultureI
我使用此代码将格式为“yyyy-MM-ddTHH:mm:ssZ”的 UTC 字符串时间转换为日期时间。例如: string PurchaseDate == "2017-12-12T14:29:26Z"
我有一个制表符分隔的文件,它正在被解析然后插入到数据库中。当我遇到日期列时,我无法解析它。 我的代码是: var insert = DateTime.ParseExact(line[i], "d/M/
这个问题在这里已经有了答案: DateTime conversion from string C# (3 个答案) 关闭 5 年前。 我有一个没有字段分隔符的时间和日期值,我试图使用 TryPars
为什么以下代码会生成 FormatException? DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", null); 也许这与代码在 IIS 7.5 E
我正在尝试使用 ParseExact() 方法解析字符串: DateTime.ParseExact(@"UTC 2014-07-08 13:11:40.396", @"UTC yyyy-MM-dd H
我正在尝试使用单个格式字符串读取以下格式的日期: "1000.12.31" "999.12.31" "99.12.31" 这些对应于公元 1000 年、公元 999 年和公元 99 年的日期。 我尝试
我有一个 DateTime 的字符串表示形式,如下所示: 2011-05-25T16:42:17.156Z 我尝试了以下但没有任何运气: DateTime.ParseExact(formatted,
我用c#编译了下面的代码 string datetime = String.Format("{0:MM/dd/yyyy hh:mm:ss tt}", root.SelectSingleNode("la
我有下面的字符串,我需要将其解析为日期时间: Thu Aug 14 2014 00:00:00 GMT 0100 (GMT Summer Time) 我不确定要为我的 DateTime.ParseEx
我必须解析以小时、分钟、秒和秒的小数形式给出的时间。比如 "15:42:58.1" "15:42:58.21" "15:42:58.417" 使用以下代码: DateTime.ParseExact("
我有一个 Web 应用程序,它通过查询字符串将 DateTime 从一个页面传递到另一个页面。它在 IE 和 FireFox 中都工作得很好,但每当我在谷歌浏览器中尝试它时都会抛出异常。该程序在以下行
我有一个字符串 Value = "12/11/2013" 并尝试使用 解析它 DateTime.ParseExact(Value, "dd/MM/yyyy", null); 但是有一个异常 Forma
var str = "00:00:00 02/01/1990"; var dt = DateTime.ParseExact(str, "hh:mm:ss dd/MM/yyyy", null); 上面的
当对 DateTime 使用 .ParseExact() 方法时,我总是得到与我输入的字符串相同的输出。这是我的代码: [Authorize(Roles = "Backoffice, Mana
我的日期格式如下“hh:mm:ss:ffff dd/MM/yy”(例如...“20:58:54:5854 28/07/07”)但是当我尝试用 DateTime.ParseExact 解析它失败并出现格
我已经查看了 Parse very long date format to DateTime in C# 的答案它对解决我的问题有一点帮助,但我担心我可能会陷入一个不相关的问题,因此打开了这个新线程。
我有日期时间字符串,看起来可能类似于以下任何内容: "1 13 2009 2300", "1 14 2009 0", "1 14 2009 100" 我需要将其解析为 DateTime。 我试过: s
我有一些以日期命名的子目录,如下所示: C:\SomeDirectory\201309\01 C:\SomeDirectory\201309\02 C:\SomeDirectory\201309\03
我是一名优秀的程序员,十分优秀!