<bdo id='4OwhR'></bdo><ul id='4OwhR'></ul>
    1. <small id='4OwhR'></small><noframes id='4OwhR'>

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

        <legend id='4OwhR'><style id='4OwhR'><dir id='4OwhR'><q id='4OwhR'></q></dir></style></legend>

        将 C# 字符串解析为 DateTime

        时间:2023-09-17
          <bdo id='RvLoc'></bdo><ul id='RvLoc'></ul>

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

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

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

                <tfoot id='RvLoc'></tfoot>

                  本文介绍了将 C# 字符串解析为 DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个这样的字符串:250920111414

                  I have a string like this: 250920111414

                  我想从该字符串创建一个 DateTime 对象.截至目前,我使用子字符串并这样做:

                  I want to create a DateTime object from that string. As of now, I use substring and do it like this:

                  string date = 250920111414;
                  
                  int year = Convert.ToInt32(date.Substring(4, 4));
                  int month = Convert.ToInt32(date.Substring(2, 2));
                  ...
                  DateTime dt = new DateTime(year, month, day ...);
                  

                  是否可以使用字符串格式,做同样的事情,没有子字符串?

                  Is it possible to use string format, to do the same, without substring?

                  推荐答案

                  当然.从您的字符串猜测格式,您可以使用 ParseExact

                  Absolutely. Guessing the format from your string, you can use ParseExact

                  string format = "ddMMyyyyHHmm";
                  
                  DateTime dt = DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
                  

                  TryParseExact:

                  DateTime dt;
                  bool success = DateTime.TryParseExact(value, format, 
                                       CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
                  

                  后一个调用将在解析失败时简单地返回 false,而不是抛出异常 - 如果您可能有不应该导致整个任务失败的错误数据(例如,它是用户输入,并且你只是想提示他们)那么这是一个更好的调用.

                  The latter call will simply return false on parse failure, instead of throwing an exception - if you may have bad data which shouldn't cause the overall task to fail (e.g. it's user input, and you just want to prompt them) then this is a better call to use.

                  有关格式字符串详细信息的更多详细信息,请参阅 "自定义日期和MSDN 中的时间格式字符串".

                  For more details about the format string details, see "Custom Date and Time Format Strings" in MSDN.

                  这篇关于将 C# 字符串解析为 DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                    1. <tfoot id='UR48W'></tfoot>

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

                          <tbody id='UR48W'></tbody>