如何将ISO8601 TimeSpan转换为c# TimeSpan?

时间:2022-10-30 15:35:48

I am getting a System.FormatException when I try to do the following (as an example):

我得到了一个系统。FormatException用于尝试执行以下操作(作为示例):

TimeSpan ts = XmlConvert.ToTimeSpan("P72H");

I've investigated the ISO8601 Standard and 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

3 个解决方案

#1


51  

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/#持续时间

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 nmn能否,在纽约代表了许多年,海里的月数,nD的天数,“T”是日期/时间分隔符,nH的小时数,海里的分钟数和nS的秒数。秒数可以包括任意精度的十进制数字。

#2


9  

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 TimeSpan that stringifies to 1.01:00:00.)

(对一个只能到凌晨1点的TimeSpan来说)。

#3


1  

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: 14S

System.Xml.XmlConvert.ToTimeSpan("P2Y4M6DT8H14M18S")

请对System.Xml.XmlConvert.ToTimeSpan(“pnynmndhnmns”)使用以下格式。标签必须在日期格式之前放置。纽约——许多年,例:2 y nM -几个月前:4 m nD -交货天数:6 d T -前的指示器,必须放置时间格式nH -的小时数例:8 h nM -分钟交货数量:12 m nS - ex的秒数:14年代System.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.

在这里,混淆部分与月和分钟有相同的字母来表示,但是指定器的用法使它们分开,便于理解。

#1


51  

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/#持续时间

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 nmn能否,在纽约代表了许多年,海里的月数,nD的天数,“T”是日期/时间分隔符,nH的小时数,海里的分钟数和nS的秒数。秒数可以包括任意精度的十进制数字。

#2


9  

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 TimeSpan that stringifies to 1.01:00:00.)

(对一个只能到凌晨1点的TimeSpan来说)。

#3


1  

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: 14S

System.Xml.XmlConvert.ToTimeSpan("P2Y4M6DT8H14M18S")

请对System.Xml.XmlConvert.ToTimeSpan(“pnynmndhnmns”)使用以下格式。标签必须在日期格式之前放置。纽约——许多年,例:2 y nM -几个月前:4 m nD -交货天数:6 d T -前的指示器,必须放置时间格式nH -的小时数例:8 h nM -分钟交货数量:12 m nS - ex的秒数:14年代System.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.

在这里,混淆部分与月和分钟有相同的字母来表示,但是指定器的用法使它们分开,便于理解。