How to comvert "20030205195847Z" to DateTime

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
 
J

Jon Skeet [C# MVP]

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

Peter van der Goes

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

Jon Skeet [C# MVP]

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);
}
}
 
J

Jerry Pisk

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

Jerry
 
J

Jon Skeet [C# MVP]

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

Marc Scheuner [MVP ADSI]

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
 
J

Jon Skeet [C# MVP]

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>
 
P

Peter van der Goes

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!
 

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