本文介绍了使用DateTime.ParseExact的意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下代码:


result = DateTime.ParseExact(" 1999-12) -01T23:59:59Z"," yyyy-MM-ddTHH:mm:ssZ",

CultureInfo.InvariantCulture);


我进去的地方结果结果 02.12.1999 00:59:59而不是teh

预计" 01.12.1999 23:59:59"

有人有想法吗?


Thanx Peter

Hi,

I''ve the following line of code:

result = DateTime.ParseExact("1999-12-01T23:59:59Z", "yyyy-MM-ddTHH:mm:ssZ",
CultureInfo.InvariantCulture);

where I get in result "result" "02.12.1999 00:59:59" instead of teh
expected "01.12.1999 23:59:59"
Anybody a idea ?

Thanx Peter

推荐答案



result = DateTime.ParseExact(" 1999-12-01T23:59:59-5",

" yyyy-MM -ddTHH:mm:ssz",CultureInfo.InvariantCulture);


给出了正确的结果(我在GMT-5)。


Arne

result = DateTime.ParseExact("1999-12-01T23:59:59-5",
"yyyy-MM-ddTHH:mm:ssz", CultureInfo.InvariantCulture);

gives the correct result for me (I am in GMT-5).

Arne




问题是Z显然会影响解析。


result = DateTime.ParseExact(" 1999-12) -01T23:59:59",

" yyyy-MM-ddTHH:mm:ss",CultureInfo.InvariantCulture);

Console.WriteLine(result);

result = DateTime.ParseExact(" 1999-12-01T23:59:59Z",

" yyyy-MM-ddTHH:mm:ssZ",CultureInfo.InvariantCulture );

Console.WriteLine(结果);


输出:


01-12-1999 23: 59:59

01-12-1999 18:59:59

The problem is that Z apperently do effect the parsing.

result = DateTime.ParseExact("1999-12-01T23:59:59",
"yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(result);
result = DateTime.ParseExact("1999-12-01T23:59:59Z",
"yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
Console.WriteLine(result);

outputs:

01-12-1999 23:59:59
01-12-1999 18:59:59



No.


result = DateTime.ParseExact(" 1999-12-01T23:59:59-5" ,

" yyyy-MM-ddTHH:mm:ssz",CultureInfo.InvariantCulture);

Console.WriteLine(result);

result = DateTime.ParseExact(" 1999-12-01T23:59:59 + 0",

" yyyy-MM-ddTHH:mm:ssz",CultureInfo.InvariantCulture);

Console.WriteLine(结果);


输出:


01-12-1999 23:59:59

01-12-1999 18:59:59

Arne

No.

result = DateTime.ParseExact("1999-12-01T23:59:59-5",
"yyyy-MM-ddTHH:mm:ssz", CultureInfo.InvariantCulture);
Console.WriteLine(result);
result = DateTime.ParseExact("1999-12-01T23:59:59+0",
"yyyy-MM-ddTHH:mm:ssz", CultureInfo.InvariantCulture);
Console.WriteLine(result);

outputs:

01-12-1999 23:59:59
01-12-1999 18:59:59

Arne




问题是Z显然会影响解析。


result = DateTime.ParseExact(" 1999-12) -01T23:59:59"," yyyy-MM-ddTHH:mm:ss",

CultureInfo.InvariantCulture);

Console.WriteLine(result);

result = DateTime.ParseExact(" 1999-12-01T23:59:59Z",

" yyyy-MM-ddTHH:mm:ssZ",CultureInfo.InvariantCulture );

Console.WriteLine(结果);


输出:


01-12-1999 23: 59:59

01-12-1999 18:59:59


The problem is that Z apperently do effect the parsing.

result = DateTime.ParseExact("1999-12-01T23:59:59", "yyyy-MM-ddTHH:mm:ss",
CultureInfo.InvariantCulture);
Console.WriteLine(result);
result = DateTime.ParseExact("1999-12-01T23:59:59Z",
"yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
Console.WriteLine(result);

outputs:

01-12-1999 23:59:59
01-12-1999 18:59:59



No.


result = DateTime.ParseExact(" 1999-12-01T23:59:59-5" ,

" yyyy-MM-ddTHH:mm:ssz",CultureInfo.InvariantCulture);

Console.WriteLine(result);

result = DateTime.ParseExact(" 1999-12-01T23:59:59 + 0",

" yyyy-MM-ddTHH:mm:ssz",CultureInfo.InvariantCulture);

Console.WriteLine(结果);


输出:


01-12-1999 23:59:59

01-12-1999 18:59:59

Arne


No.

result = DateTime.ParseExact("1999-12-01T23:59:59-5",
"yyyy-MM-ddTHH:mm:ssz", CultureInfo.InvariantCulture);
Console.WriteLine(result);
result = DateTime.ParseExact("1999-12-01T23:59:59+0",
"yyyy-MM-ddTHH:mm:ssz", CultureInfo.InvariantCulture);
Console.WriteLine(result);

outputs:

01-12-1999 23:59:59
01-12-1999 18:59:59

Arne


这篇关于使用DateTime.ParseExact的意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:42