How to comvert "20030205195847Z" to DateTime

  • Thread starter Thread starter Rudy Ko
  • Start date Start date
R

Rudy Ko

To All,

I have a timestamp value equals to "20030205195847Z" that I retrieved
from a LDAP property. I got an exception "String was not recognized as a
valid DateTime" when I used Convert.ToDateTime(). Do you know how to convert
it to a DateTime.

Regards,
Rudy
 
Rudy Ko said:
I have a timestamp value equals to "20030205195847Z" that I retrieved
from a LDAP property. I got an exception "String was not recognized as a
valid DateTime" when I used Convert.ToDateTime(). Do you know how to convert
it to a DateTime.

Try DateTime.ParseExact using an appropriate format string - see
"custom date and time format strings" for information about the format
string.
 
Rudy Ko said:
To All,

I have a timestamp value equals to "20030205195847Z" that I retrieved
from a LDAP property. I got an exception "String was not recognized as a
valid DateTime" when I used Convert.ToDateTime(). Do you know how to convert
it to a DateTime.

Regards,
Rudy
First, I suggest you use the DateTime class Parse() method to parse the
string. You'll need to convert your string to a format acceptable to
Parse(), like:

String s = "02/05/2003 19:58:47Z";

which works fine.
 
First, I suggest you use the DateTime class Parse() method to parse the
string. You'll need to convert your string to a format acceptable to
Parse(), like:

String s = "02/05/2003 19:58:47Z";

which works fine.

There's no need to change the string's format to start with. Try this:

using System;
using System.Globalization;

class Test
{
static void Main()
{
string date = "20030205195847Z";
string format = "yyyyMMddHHmmss'Z'";
DateTime dt = DateTime.ParseExact
(date, format, CultureInfo.InvariantCulture);
Console.WriteLine(dt);
}
}
 
Just a little note - that Z at the end of the time string is probably a time
zone, not a literal Z character.

Jerry
 
Jerry Pisk said:
Just a little note - that Z at the end of the time string is probably a time
zone, not a literal Z character.

Yes, I was going to write something about that, but didn't have time. I
seem to remember there are problems with DateTime.ParseExact
recognising Z correctly, but can't remember the details. If *all* the
strings passed in are in UTC, it's probably easiest to use the literal
as I did. Otherwise, the OP will have to look at getting the time zone
right.
 
Just a little note - that Z at the end of the time string is probably a time
zone, not a literal Z character.

No, not really - it stands for "Zulu" time, AFAIK. Which denotes what
used to be called "Zulu" or "military" time - e.g. hours in 24-hour
format (rather than the US 12-hour am/pm) format.

I like to call that "standard time format" since the whole world
(except for North America) uses it ;-)

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Marc Scheuner said:
No, not really - it stands for "Zulu" time, AFAIK. Which denotes what
used to be called "Zulu" or "military" time - e.g. hours in 24-hour
format (rather than the US 12-hour am/pm) format.

No, I don't believe so.

See http://www.cl.cam.ac.uk/~mgk25/iso-time.html

From the above:

<quote>
[The Z stands for the "zero meridian", which goes through Greenwich in
London, and it is also commonly used in radio communication where it is
pronounced "Zulu" (the word for Z in the international radio alphabet).
Universal Time (sometimes also called "Zulu Time") was called Greenwich
Mean Time (GMT) before 1972, however this term should no longer be
used. Since the introduction of an international atomic time scale,
almost all existing civil time zones are now related to UTC, which is
slightly different from the old and now unused GMT.]
</quote>
 
Jon Skeet said:
There's no need to change the string's format to start with. Try this:

using System;
using System.Globalization;

class Test
{
static void Main()
{
string date = "20030205195847Z";
string format = "yyyyMMddHHmmss'Z'";
DateTime dt = DateTime.ParseExact
(date, format, CultureInfo.InvariantCulture);
Console.WriteLine(dt);
}
}

Cool!
Amazing what I learn hanging out in groups you frequent, John.
Thanks very much!
 
Back
Top