如何转换的时间跨度ISO8601为C#时间跨度? [英] How do I convert an ISO8601 TimeSpan to a C# TimeSpan?

查看:797
本文介绍了如何转换的时间跨度ISO8601为C#时间跨度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个 System.FormatException 当我尝试做以下(作为一个例子):

 时间跨度TS = XmlConvert.ToTimeSpan(P72H);

我已经调查了 ISO8601标准,它似乎是正确的,但我不能让它解析小时没有抛出任何异常,无论我做什么。

谢谢!

编辑:

异常详细信息:

  System.FormatException是由用户code未处理
的HResult = -2146233033
消息=字符串'P72H'不是一个有效的时间跨度值。
来源=的System.Xml


解决方案

您需要的时间分隔符添加到您的字符串。试试这个:

 时间跨度TS = XmlConvert.ToTimeSpan(PT72H);

查看时间规范 - http://www.w3.org/TR/xmlschema-2/#持续时间


  

3.2.6.1词汇重新presentation结果
  持续时间的词汇重新presentation是[ISO 8601]扩展格式PnYn MnDTnH NMNS,在纽约重新presents的年数,NM月数,第二的天数,T是日期/时间分隔,NH的小时数,纳米分钟的纳秒数和秒数。秒数可以包含小数位数任意precision。


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

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.

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

解决方案

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

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.

这篇关于如何转换的时间跨度ISO8601为C#时间跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆