<tfoot id='KbaCo'></tfoot>

    <i id='KbaCo'><tr id='KbaCo'><dt id='KbaCo'><q id='KbaCo'><span id='KbaCo'><b id='KbaCo'><form id='KbaCo'><ins id='KbaCo'></ins><ul id='KbaCo'></ul><sub id='KbaCo'></sub></form><legend id='KbaCo'></legend><bdo id='KbaCo'><pre id='KbaCo'><center id='KbaCo'></center></pre></bdo></b><th id='KbaCo'></th></span></q></dt></tr></i><div id='KbaCo'><tfoot id='KbaCo'></tfoot><dl id='KbaCo'><fieldset id='KbaCo'></fieldset></dl></div>
      <bdo id='KbaCo'></bdo><ul id='KbaCo'></ul>

    1. <legend id='KbaCo'><style id='KbaCo'><dir id='KbaCo'><q id='KbaCo'></q></dir></style></legend>

      1. <small id='KbaCo'></small><noframes id='KbaCo'>

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

        时间:2023-08-28

              <tbody id='89EmL'></tbody>
            • <bdo id='89EmL'></bdo><ul id='89EmL'></ul>
            • <i id='89EmL'><tr id='89EmL'><dt id='89EmL'><q id='89EmL'><span id='89EmL'><b id='89EmL'><form id='89EmL'><ins id='89EmL'></ins><ul id='89EmL'></ul><sub id='89EmL'></sub></form><legend id='89EmL'></legend><bdo id='89EmL'><pre id='89EmL'><center id='89EmL'></center></pre></bdo></b><th id='89EmL'></th></span></q></dt></tr></i><div id='89EmL'><tfoot id='89EmL'></tfoot><dl id='89EmL'><fieldset id='89EmL'></fieldset></dl></div>

              <small id='89EmL'></small><noframes id='89EmL'>

                <legend id='89EmL'><style id='89EmL'><dir id='89EmL'><q id='89EmL'></q></dir></style></legend>
              1. <tfoot id='89EmL'></tfoot>

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

                  问题描述

                  当我尝试执行以下操作(例如)时,我得到了一个 System.FormatException:

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

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

                  我调查了 ISO8601 标准,它似乎是正确的,但我无论我做什么,都无法让它在不抛出异常的情况下解析时间.

                  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.

                  谢谢!

                  异常详情:

                  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");
                  

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

                  See the duration specification - http://www.w3.org/TR/xmlschema-2/#duration

                  3.2.6.1 词法表示
                  持续时间的词法表示是 [ISO 8601] 扩展格式 PnYn MnDTnH nMnS,其中 nY 表示年数,nM 表示月数,nD 表示天数,'T' 是日期/时间分隔符,nH 表示数字小时数,nM 分钟数和 nS 秒数.秒数可以包含任意精度的十进制数字.

                  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.

                  根据评论编辑/更新

                  由于存在一些问题,为什么字符串 P2M2W5D 不会被视为有效的 TimeSpan,因为 W 是 ISO 8601 的一部分标准,我想添加此更新,以便如果有人遇到该问题,他们不必通读评论即可获得答案.对于有问题的原始字符串 P72HP2M2W5D 的问题是该字符串必须符合 W3C XML Schema(请参阅 XmlConvert.ToTimeSpan).当我们查看 W3C XML Schema(上面的链接)时,它引用了 ISO 8601 标准,特别是第 5.5.3.2.1 节,它给出了 W 不是有效字符的原因在 XML 架构中:

                  As there was some question as to why the string P2M2W5D would not be considered a valid TimeSpan since W is 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 P72H and P2M2W5D is 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 W is not a valid character in the XML Schema:

                  由于周没有明确的结转点(52 或 53),因此周应不能在这些应用程序中使用

                  Since weeks have no defined carry-over point (52 or 53), weeks should not be used in these applications

                  这篇关于如何将 ISO8601 TimeSpan 转换为 C# TimeSpan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C# Google Drive APIv3 上传文件 下一篇:如何用 C# 编写解析器?

                  相关文章

                  最新文章

                    <small id='iG86P'></small><noframes id='iG86P'>

                    <i id='iG86P'><tr id='iG86P'><dt id='iG86P'><q id='iG86P'><span id='iG86P'><b id='iG86P'><form id='iG86P'><ins id='iG86P'></ins><ul id='iG86P'></ul><sub id='iG86P'></sub></form><legend id='iG86P'></legend><bdo id='iG86P'><pre id='iG86P'><center id='iG86P'></center></pre></bdo></b><th id='iG86P'></th></span></q></dt></tr></i><div id='iG86P'><tfoot id='iG86P'></tfoot><dl id='iG86P'><fieldset id='iG86P'></fieldset></dl></div>
                  1. <tfoot id='iG86P'></tfoot>
                  2. <legend id='iG86P'><style id='iG86P'><dir id='iG86P'><q id='iG86P'></q></dir></style></legend>
                      • <bdo id='iG86P'></bdo><ul id='iG86P'></ul>