zoukankan      html  css  js  c++  java
  • [原创]DateTime在使用 format Custom Date and Time Format Strings时遇到的问题和解决方法

    Please see the following code first:

    More information about Custom Date and Time Format Strings:

    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#zzSpecifier

    Date and Time Format Strings

    http://mis2.comm.virginia.edu/grazioli/documents/date%20and%20time%20format%20strings.htm

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Xml;

    namespace ConsoleApplication10
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    string datetime = "12/31/9999 11:59:59 PM";
               
    // string datetime = "12/30/9999 11:59:59 PM"; this will be ok, why?

                DateTime tempDateTime1;
                DateTime tempDateTime2;

                
    string test = string.Empty; ;

                
    if (DateTime.TryParse(datetime, out tempDateTime2) == true)
                {
                  test = XmlConvert.ToString(tempDateTime2, "yyyy-MM-ddTHH:mm:ssZ");//isValid will be false
                  
    // test = XmlConvert.ToString(tempDateTime1, "yyyy-MM-ddTHH:mm:ss z"); isValid will be true
                  //test = XmlConvert.ToString(tempDateTime1, "yyyy-MM-ddTHH:mm:ss"); //isValid will be true   
                  // test = XmlConvert.ToString(tempDateTime1, "yyyy-MM-ddTHH:mm:ssz"); isValid will be true 
                }

                
    bool isValid1 = DateTime.TryParse(test, out tempDateTime1);
                
    bool nochange = (tempDateTime2 == tempDateTime1);

                Convert.ToDateTime(test)
              
            }
        }
    }

     //http://stackoverflow.com/questions/1728404/date-format-yyyy-mm-ddthhmmssz

    // In this format Z is time zone. //T is long time pattern 
    // If the time is in UTC, add a 'Z' directly after the time without a space. 'Z' is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "144515Z".


    // Two things to note: + or - is needed after the time but obviously + doesn't show on positive numbers. According to wikipedia the offset can be in +hh format or +hh:mm. I've kept to just hours.  
     

    The original code is: XmlConvert.ToString(tempDateTime, "yyyy-MM-ddTHH:mm:ssZ");

     

     In this format Z is time zone. T is long time pattern  Add a 'Z' directly after the time without a space means this time is UTC time.

     

    The following is the reason why the issue happen:

    1): string datetime = "12/31/9999 11:59:59 PM";   The time is used as a UTC time when use format "yyyy-MM-ddTHH:mm:ss”.

    2): DateTime.TryParse:  use the currentInfo which includes the currentCulture.dateTimeInfo, so when the UTC time converted to local time, out of range exception is happened.

     

    To resolve this issue, we can remove the Z or use “z”. With DateTime values, the "z" custom format specifier represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. In this case, If we use yyyy-MM-ddTHH:mm:ssz, the value of test is “9999-12-31T23:59:59 +8”, the +8 means the offset offset of the local operating system's time zone from Coordinated Universal Time (UTC).

    做个快乐的自己。
  • 相关阅读:
    docker log
    byte转String防止乱码
    SQL索引
    Redis 总结精讲
    如何保证消息队列是高可用的
    消息中间件(一)MQ详解及四大MQ比较
    @Bean和@Componet区别
    理解Spring的AOP和Ioc/DI就这么简单
    SpringBoot 基础
    《Linux 鸟哥私房菜》 第6章 Linux的文件权限与目录配置
  • 原文地址:https://www.cnblogs.com/Jessy/p/1868046.html
Copyright © 2011-2022 走看看