随笔 - 436,  文章 - 151,  评论 - 86,  阅读 - 136万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

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).

posted on   higirle  阅读(905)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· SQL Server统计信息更新会被阻塞或引起会话阻塞吗?
阅读排行:
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 本地部署 DeepSeek:小白也能轻松搞定!
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)
点击右上角即可分享
微信分享提示