PC Review


Reply
Thread Tools Rate Thread

How to comvert "20030205195847Z" to DateTime

 
 
Rudy Ko
Guest
Posts: n/a
 
      23rd Jun 2004
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



 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jun 2004
Rudy Ko <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Peter van der Goes
Guest
Posts: n/a
 
      23rd Jun 2004

"Rudy Ko" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jun 2004
Peter van der Goes <(E-Mail Removed)> wrote:
> > 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.


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

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jerry Pisk
Guest
Posts: n/a
 
      24th Jun 2004
Just a little note - that Z at the end of the time string is probably a time
zone, not a literal Z character.

Jerry

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter van der Goes <(E-Mail Removed)> wrote:
>> > 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.

>
>> 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);
> }
> }
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      24th Jun 2004
Jerry Pisk <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Marc Scheuner [MVP ADSI]
Guest
Posts: n/a
 
      24th Jun 2004
>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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      24th Jun 2004
Marc Scheuner [MVP ADSI] <(E-Mail Removed)> wrote:
> >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.


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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Marc Scheuner [MVP ADSI]
Guest
Posts: n/a
 
      24th Jun 2004
>> No, not really - it stands for "Zulu" time, AFAIK.
>
>No, I don't believe so.
>See http://www.cl.cam.ac.uk/~mgk25/iso-time.html


Ah, okay, thanks a lot ! Learned something again..... ;-)

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Reply With Quote
 
Peter van der Goes
Guest
Posts: n/a
 
      24th Jun 2004

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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);
> }
> }
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


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

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Field Names: "LongName", "ShortName", "Code", "Description","Comments" PeteCresswell Microsoft Access 2 25th Feb 2009 11:41 PM
if (DateTime.Compare(Session["opens"], Session["resdate"]) > 0) =?Utf-8?B?TkNaSU1N?= Microsoft C# .NET 3 18th Jan 2005 03:37 PM
<FORM METHOD="post" onSubmit="return fieldcheck()" name="orientation" action="http://ws-kitty.BU.edu/AT/survey/orientation/script/write.asp" language="JavaScript"> Joeyej Microsoft ASP .NET 0 4th Jun 2004 08:55 PM
What is an easy way to create DateTime from a string that contains date in the following format "yyyyMMdd"? Arild Bakken Microsoft C# .NET 2 24th Sep 2003 03:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:21 AM.