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.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(tempDateTime1, "yyyy-MM-ddTHH:mm:ss z"); isValid will be true
//test = XmlConvert.ToString(tempDateTime1, "yyyy-MM-ddTHH:mm:ss"); //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). |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· SQL Server统计信息更新会被阻塞或引起会话阻塞吗?
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 本地部署 DeepSeek:小白也能轻松搞定!
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)