<分区>
来自 ISO-8601:2004(E) 规范:
4.2.2.4 Representations with decimal fraction
If necessary for a particular application a decimal fraction of hour, minute or second may be included. If a decimal fraction is included, lower order time elements (if any) shall be omitted and the decimal fraction shall be divided from the integer part by the decimal sign specified in ISO 31-0, i.e. the comma [,] or full stop [.]. Of these, the comma is the preferred sign.
很简单。所以根据这个规范,秒的小数部分最好用逗号分隔整数和小数部分,例如 2014-01-01T00:00:00,123
。然而,似乎几乎所有地方都只接受小数点(又名“句号”)!
现在我确定有一些语言或库考虑到了这一点,而且我知道在很多情况下您可以自己提供格式的完整详细信息。但这似乎是对规范的明显疏忽,似乎很多程序员都犯了同样的错误。 除了纯粹的人为错误之外,还有其他原因吗?
下面是我测试过的列表。如果您发现任何其他问题,请随时编辑问题以扩充我的列表。谢谢。
.NET/C#
DateTime dt = DateTime.Parse("2014-01-01T00:00:00,123");
抛出一个 FormatException
消息“String was not recognized as a valid DateTime”。使用句点而不是逗号的相同内容解析成功。
JavaScript 日期对象
在最新的(截至撰写本文时)Chrome、Internet Explorer、Firefox 和 Node.js 中测试:
var dt = new Date('2014-01-01T00:00:00,123');
返回 “无效日期”
。改用句点效果很好。
JavaScript moment.js
var valid = moment("2014-01-01T00:00:00,123").isValid();
返回 false
。使用句点代替返回 true
。
PHP
echo strtotime('2014-01-01T00:00:00,123');
返回一个空字符串。改用句点效果很好。
ruby
require 'time'
puts Time.iso8601("2014-01-01T00:00:00,123")
给出一个运行时错误。虽然 Time
不保留小数秒,但它不应该出错 - 事实上,如果使用句点代替,它会起作用。