Unexpected result using DateTime.ParseExact

  • Thread starter Peter Holschbach
  • Start date
P

Peter Holschbach

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Peter said:
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);

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

Arne
 
P

Peter Holschbach

Hi Arne,

thanx for this info. In your example you use "z" and I use capitol "Z".
Having a look into the help the capitol letter "Z" is not a special
character and I hoped that it will be ignored but it seems to be as if it is
interpreded. Maybe a bug in the documentaion.

thanx
Peter
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Peter said:
thanx for this info. In your example you use "z" and I use capitol "Z".
Having a look into the help the capitol letter "Z" is not a special
character and I hoped that it will be ignored but it seems to be as if
it is interpreded. Maybe a bug in the documentaion.

I thought you did want a timezone.

The format Z does match the literal Z, but apparently it
is enough to make the method think it is timezone GMT.

If you really have a literal Z then I think you should strip
it off before parsing.

Arne
 
S

Stephany Young

No, not a bug in the documentation.

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

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".
 
?

=?windows-1252?Q?Arne_Vajh=F8j?=

Stephany said:
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).

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

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(result);

outputs:

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

Arne
 
S

Stephany Young

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

Cor Ligthert[MVP]

Stephany,

Zulu comes from Zero. I thought that military was only used in the USA, GMT
is at least in Europe more common for the official UTC. Have I understand it
well that the in my idea very Gemenebest country New Sealand uses as well
"Military"?

I don't by the way understand why Peter wants to get another date then he
has written.

In the very spread area CET it now (Not Summer Time) exactly one our later
then in UTC/GMT/ZULU/Military.

For your information: I wrote "spread area" this is because by instance the
Netherlands should be geographicaly in GMT. However (AFAIK) after the last
war they left it in CET because the whole western part of European continent
is now almost in it.

Cor
 
S

Stephany Young

No Cor!

Zulu (UTC) comes from the last letter of the NATO phonetic alphabet, (Alpha,
Bravo, Charlie, ..., Xray, Yankee, Zulu).

It is the standard designator for the time base in aviation throughout the
world.

It's got absoulutely nothing to do with CET, the Netherlands, the
Commonwealth or even New Zealand for that matter.
 
C

Cor Ligthert [MVP]

Stephany,

GMT/UTC/Militairy is Zero time, as you wrote it has to do with the phonetic
alphabet, however I thought, let I not write that to Stephany, because then
I get a correction.

It is created in Greenwich, there is a nice building that you can see when
you go from London upwards to where Jon Skeet is living at your right side.
That building is at the Zero time of the world.

:)

Cor
 
C

Cor Ligthert [MVP]

Stephany,

And to make even three answers on your message.

The OP is from the CET timezone like me, and therefore his time is 1 hour
later then UTC/CET/Zulu/Zero/Military time.

My later part was as well meant for the OP.

Cor
 
S

Stephany Young

If you want to argue on this particular subject then go away and do some
research first and then form your argument into coherent English sentences.

GMT/UTC, CET, EST, PST etc. are abbreviations for time zone labels used in
most civilian situation situations.

NATO and a lot of other military organizations that are not part of NATO use
a letter of the (military) phonetic alphabet (but not J) to indicate the 25
meridians, A)lpha, B)ravo, C)harlie, ..., X)ray, Y)ankee, Z)ulu. The
(military) Zulu meridian coincides with the prime meridian which also
happens to be the GMT/UTC meridian.

The global aviation industry uses 'Zulu' for exactly the same purpose as the
military but they use 'Zulu' no matter where they are in the world. This
means that pilots and air traffic controllers never have to do a time zone
change or converison and this is a big factor in minimising the risk of
aircraft collisions and crashes.

But don't just take my word for it. If you know any pilots and/or air trafic
controllers and/or soldiers then ask them about Zulu time.
 
C

Cor Ligthert[MVP]

Hi Stephany,

It is now 19:00 in the Netherlands which is as I wrote in CET and that is
one hour later then in England where it is 18:00.

(It is however earlier 12:00 here then in England, because CET is 1 hour
before GMT, that is probably your confusion, however I wrote nowhere that
CET is behind GMT/UTC/ZULU/Zero time/Military).

BBC 1 and 2 are standard television stations here and I can assure you that
I don't have to think when they are talking about the time and that is
forever because in the EU is the summertime (what is in the US timesaving
time) synchronized for every timetable.

Cor
 
P

Peter Holschbach

Hi All,

thank you very much for all the answers (really a interesting thread and
helpful !!!).
Someone asked why to hell I didn't want to have the right result :)

1) I have to support a application which stores the time informatin wrong
(ends with Zulu but local time is stored)
2) looking into the help for DateTime.FormatExact I could not find why the
function behaves as it does.

I asked the same question in the german NG and got an answer with many links
concerning this problem.
Maybe they are helpfule for you too. Here they are:

<http://msdn2.microsoft.com/en-us/library/91hfhz89.aspx>
<http://msdn2.microsoft.com/en-us/netframework/aa569606.aspx>
<http://msdn2.microsoft.com/en-us/library/ms973825.aspx>
<http://msdn2.microsoft.com/en-us/library/2h3syy57.aspx>
<http://msdn2.microsoft.com/en-us/library/5hh873ya.aspx>
<http://msdn2.microsoft.com/en-us/library/8kb3ddd4.aspx>

<http://blogs.msdn.com/bclteam/archive/2004/12/20/327702.aspx>

<URL:http://blogs.msdn.com/bclteam/archive/2007/06/18/a-brief-history-of-datetime-anthony-moore.aspx
<http://blogs.msdn.com/bclteam/archive/2007/06/18/a-brief-history-of-datetime-anthony-moore.aspx>>
<URL:http://blogs.msdn.com/bclteam/archi...tory-of-datetime-follow-up-anthony-moore.aspx
<http://blogs.msdn.com/bclteam/archi...tory-of-datetime-follow-up-anthony-moore.aspx>>

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top