[原创]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

 

ExpandedBlockStart.gif 代码
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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值