如何将 ISO8601 时间跨度转换为 C# 时间跨度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12466188/
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
How do I convert an ISO8601 TimeSpan to a C# TimeSpan?
提问by Codeman
I am getting a System.FormatExceptionwhen I try to do the following (as an example):
System.FormatException当我尝试执行以下操作时,我得到了一个(例如):
TimeSpan ts = XmlConvert.ToTimeSpan("P72H");
I've investigated the ISO8601 Standardand it seems to be correct, but I cannot get it to parse hours without throwing an exception, no matter what I do.
我已经调查了ISO8601 标准,它似乎是正确的,但是无论我做什么,我都无法在不抛出异常的情况下解析数小时。
Thanks!
谢谢!
EDIT:
编辑:
Exception detail:
异常详情:
System.FormatException was unhandled by user code
HResult=-2146233033
Message=The string 'P72H' is not a valid TimeSpan value.
Source=System.Xml
采纳答案by pstrjds
You need to add the Time separator to your string. Try this:
您需要将时间分隔符添加到您的字符串中。尝试这个:
TimeSpan ts = XmlConvert.ToTimeSpan("PT72H");
See the duration specification - http://www.w3.org/TR/xmlschema-2/#duration
请参阅持续时间规范 - http://www.w3.org/TR/xmlschema-2/#duration
3.2.6.1 Lexical representation
The lexical representation for duration is the [ISO 8601] extended format PnYn MnDTnH nMnS, where nY represents the number of years, nM the number of months, nD the number of days, 'T' is the date/time separator, nH the number of hours, nM the number of minutes and nS the number of seconds. The number of seconds can include decimal digits to arbitrary precision.
3.2.6.1 词法表示
持续时间的词法表示是 [ISO 8601] 扩展格式 PnYn MnDTnH nMnS,其中 nY 表示年数,nM 月数,nD 天数,'T' 是日期/时间分隔符,nH 小时数,nM 分钟数,nS 秒数。秒数可以包括任意精度的十进制数字。
Edit/Update based on comments
根据评论编辑/更新
As there was some question as to why the string P2M2W5Dwould not be considered a valid TimeSpansince Wis part of the ISO 8601 standard, I wanted to add this update so that if someone runs across that issue they don't have to read through the comments to get the answer. The issue, both for the original string in question P72Hand P2M2W5Dis that the string must conform to the W3C XML Schema (see the documentation for XmlConvert.ToTimeSpan). When we look at the W3C XML Schema (link above), it references back to the ISO 8601 standard, and in particular to section 5.5.3.2.1 which gives the reason why Wis not a valid character in the XML Schema:
由于存在一些关于为什么字符串P2M2W5D不被视为有效的问题,TimeSpan因为它W是 ISO 8601 标准的一部分,我想添加此更新,以便如果有人遇到该问题,他们不必通读评论得到答案。这个问题,无论是对有问题的原始字符串P72H,并P2M2W5D是该字符串必须符合W3C的XML Schema(请参阅文档XmlConvert.ToTimeSpan)。当我们查看 W3C XML 模式(上面的链接)时,它参考了 ISO 8601 标准,特别是第 5.5.3.2.1 节,它给出了为什么W在 XML 模式中不是有效字符的原因:
Since weeks have no defined carry-over point (52 or 53), weeks should not be used in these applications
由于周没有定义的结转点(52 或 53),因此不应在这些应用程序中使用周
回答by millimoose
You must have missed something in the standard. The following:
您一定遗漏了标准中的某些内容。下列:
System.Xml.XmlConvert.ToString(TimeSpan.FromHours(12))
gives me the string PT12H. So it seems like the time portion needs to be prefixed with a T. And the following parses correctly:
给我字符串PT12H。所以似乎时间部分需要以T. 以下解析正确:
System.Xml.XmlConvert.ToTimeSpan("PT25H")
(To a TimeSpanthat stringifies to 1.01:00:00.)
(对于TimeSpan字符串化为1.01:00:00.)
回答by Ravikumar B
Please use the following format for System.Xml.XmlConvert.ToTimeSpan("PnYnMnDTnHnMnS").
P - The designator must be placed before date format.
nY - Number of years, ex: 2Y
nM - Number of months ex: 4M
nD - Number of Days ex: 6D
T - The designator that must be placed before the time format
nH - Number of Hours ex: 8H
nM - Number of Minutes ex: 12M
nS - Number of seconds ex: 14SSystem.Xml.XmlConvert.ToTimeSpan("P2Y4M6DT8H14M18S")
请使用以下格式System.Xml.XmlConvert.ToTimeSpan("PnYnMnDTnHnMnS")。
P - 指示符必须放在日期格式之前。
nY - 年数,例如:2Y
nM - 月数例如:4M
nD - 天数例如:6D
T - 必须放在时间格式之前的指示符
nH - 小时数例如:8H
nM - 分钟数例如:12M
nS - 秒数 例如:14SSystem.Xml.XmlConvert.ToTimeSpan("P2Y4M6DT8H14M18S")
Here, the confusion part with Month and Minutes have the same letter to denote, but the designator usage makes them separate to understand easily.
在这里,与 Month 和 Minutes 混淆的部分使用相同的字母来表示,但指示符的用法使它们分开以便于理解。

