Re: How to comvert "20030205195847Z" to DateTime

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jerry Pisk (jerryiii_at_hotmail.com)
Date: 06/24/04


Date: Wed, 23 Jun 2004 18:52:36 -0700

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]" <skeet@pobox.com> wrote in message
news:MPG.1b43f690c740df398ad7a@msnews.microsoft.com...
> Peter van der Goes <p_vandergoes@toadstool.u> 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 - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Relevant Pages

  • Re: converting dates to intervals
    ... I don't want to consolidate to the nearest 5-min ... Put back into string w/ datestr ... Use datenumon the time string then pass that to datevec() ...
    (comp.soft-sys.matlab)
  • Re: Show date as DD/MM/YYYY in DataGridTextBoxColumn
    ... function for the DateTime object used by passing a format string. ... The date time string so obtained can be used at all ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Get minutes count
    ... David wrote: ... > How do I get time string from minutes? ... >>private string GetMinutes ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: data manipulation question
    ... KKde wrote: ... match that there are the same number of whitespace characters at the start of the string as the length of the time string and if there are replace the whitespace characters with the time string. ...
    (comp.unix.shell)
  • Re: Data Bound Custom Control
    ... Jerry ... There were many issues drawing the inner controls, ... >> Private m_name As String ... >> Public Property NameAs String ...
    (microsoft.public.dotnet.languages.vb.controls)