使用DateTime.ParseExact的意外结果 [英] Unexpected result using DateTime.ParseExact

查看:105
本文介绍了使用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

推荐答案

Peter Holschbach写道:
Peter Holschbach wrote:

我有以下代码行:


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"

有人有想法吗?
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 ?



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


Stephany Young写道:
Stephany Young wrote:

你在格式字符串中指定了''Z''( yyyy-MM-ddTHH:mm:ssZ)

因此你的值字符串必须以Z结尾,否则调用

DateTime.ParseExact将失败。 ParseExact就是这个意思(它必须实际上是
)。
You specified ''Z'' in the format string ("yyyy-MM-ddTHH:mm:ssZ")
therefore your value string MUST end with a ''Z'' otherwise the call to
DateTime.ParseExact will fail. ParseExact means just that (it must macth
EXACTLY).



问题是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


你的帖子显示你的时区是1 UTC的时间提前一小时

表示时间是UTC时间晚上11:59:59然后是第二天上午12:59:59

您所在的时区正是你得到的结果。


为了得到你想要的结果,格式字符串应该像Arne点

out一样,是yyyy-MM -ddTHH:mm:ssz",但是你的值字符串s应该是

" 1999-12-01T23:59:59 + 0"。
Your post indicates that your time zone is 1 hour ahead of UTC which
means tht when it is 11:59:59 PM UTC then it is 12:59:59 AM the next day
in your time zone which is exactly the result you got.

To get the result you want, the format string should, as Arne points
out, be "yyyy-MM-ddTHH:mm:ssz", but your value string should be
"1999-12-01T23:59:59+0".



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是否包括军方的某种指标

使用Zulu这个词来代替UTC。

" Arne Vajh?j" < ar ** @ vajhoej.dkwrote in message

news:47 *********************** @ news.sunsite。 dk ...
Yes. I seem what you mean.

I wonder if the Z is some sort of indicator included for the military who
use the term Zulu in place of UTC.
"Arne Vajh?j" <ar**@vajhoej.dkwrote in message
news:47***********************@news.sunsite.dk...

Stephany Young写道:
Stephany Young wrote:

>你指定了''Z''格式字符串(yyyy-MM-ddTHH:mm:ssZ)因此你的值字符串必须以Z结尾,否则对DateTime.ParseExact的调用将失败。 ParseExact意味着(它必须确实)。
>You specified ''Z'' in the format string ("yyyy-MM-ddTHH:mm:ssZ") therefore
your value string MUST end with a ''Z'' otherwise the call to
DateTime.ParseExact will fail. ParseExact means just that (it must macth
EXACTLY).



问题是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


>您的帖子表明您的时区比UTC早一个小时,这意味着当它是UTC时间晚上11:59:59然后是第二天上午12:59:59
在你的时区这正是你得到的结果。

为了获得你想要的结果,格式字符串应该像Arne所指出的那样,是yyyy-MM-ddTHH:mm:ssz,但你的值字符串应该是
199 9-12-01T23:59:59 + 0" ;.
>Your post indicates that your time zone is 1 hour ahead of UTC which
means tht when it is 11:59:59 PM UTC then it is 12:59:59 AM the next day
in your time zone which is exactly the result you got.

To get the result you want, the format string should, as Arne points out,
be "yyyy-MM-ddTHH:mm:ssz", but your value string should be
"1999-12-01T23:59:59+0".



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的意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆