[原创]DateTime在使用 format Custom Date and Time Format Strings时遇到的问题和解决方法
2010-11-03 14:18
831 查看
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
代码
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”.
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). |
相关文章推荐
- 使用Navicat Premium将Oracle数据库中的表和数据迁移到MySQL数据库中,遇到的Date类型出现精度问题及解决方法
- Custom Date and Time Format Strings
- Custom Date and Time Format Strings
- J2ME使用WTK模拟器开发时候遇到“OK to use Airtime” 问题的解决方法
- mybatis使用过程遇到的一些问题及解决方法
- 字符串转日期 遇到Is not a valid date and time的解决
- OpenResty中遇到"Can't locate Time/HiRes.pm in @INC"问题的解决方法
- Linux中date命令转换日期提示date: illegal time format问题解决
- 使用debmirror为Debian建立本地源的方法,以及遇到的GPG key验证错误问题的解决方法
- Java使用Oracle遇到的最大游标超出问题及其解决方法
- Macbook使用过程遇到的问题及解决方法
- 使用printf修改变量的值 —— VS2008中使用%n输出遇到的问题及解决方法
- 使用speex动态链接库过程中遇到问题及解决方法
- 记录使用python时遇到的中文 编码问题与解决方法
- 在SharePoint中服务器端使用Word编程模型转换PDF遇到的问题以及解决方法
- 使用Django内置Web服务器和sqlite3遇到的问题及解决方法
- Java使用Oracle遇到的最大游标超出问题及其解决方法
- 安装和使用安卓iGO遇到的问题及解决方法
- 使用fckeditor中遇到的几个问题及其相应解决方法
- LoadRunner在使用时遇到的问题及解决方法