Converting string to DateTime

Last post 11-06-2009 7:47 PM by Paul Linton. 8 replies.

Sort Posts:

  • Converting string to DateTime

    11-05-2009, 12:33 PM
    • Member
      7 point Member
    • dreamzor
    • Member since 12-21-2006, 8:22 AM
    • Posts 78

    Hi.

    I got a string that looks like this :

    Sun Feb 16 11:52:00 CET 2014

    Is it possible to convert this to a DateTime object ?


    Thanks.

  • Re: Converting string to DateTime

    11-05-2009, 3:54 PM

    It needs to be in this format: Sun Feb 2014 16 11:52:00 for DateTime.Parse() to work on it.  A quick and dirty way to achieve that is:

    string input = "Sun Feb 16 11:52:00 CET 2014";
    string[] temp = input.Split(' ');
    string dt = temp[0] + " " + temp[1] + " " + temp[5] + " " + temp[2] + " " + temp[3];
    Response.Write(DateTime.Parse(dt));



    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: Converting string to DateTime

    11-05-2009, 4:17 PM
    • Member
      188 point Member
    • binli0114
    • Member since 09-26-2006, 12:31 AM
    • Sydney
    • Posts 48

    try this link to see if it helps

    http://www.codeproject.com/KB/datetime/TimeZoneAwareDateTime.aspx


    ------------------------------------------------------------
    I LOVE THIS GAME
  • Re: Converting string to DateTime

    11-05-2009, 5:35 PM

    Without the "CET", it would be easy to do it.  I'm not sure how to handle the CET...

  • Re: Converting string to DateTime

    11-05-2009, 6:22 PM
    • All-Star
      91,113 point All-Star
    • SGWellens
    • Member since 01-02-2007, 9:27 PM
    • Twin Cities, MN
    • Posts 7,430
    • Moderator
      TrustedFriends-MVPs

    To skip the CET (which I assume is a time zone) you can do this:

            string Input = "Sun Feb 16 11:52:00 CET 2014";
    
            DateTime Output;
    
            if (DateTime.TryParseExact(Input, "ddd MMM dd hh:mm:ss \"CET\" yyyy", null, DateTimeStyles.None, out Output) == false)
                Output = DateTime.MinValue;
    

    There is a format/parse character (K) to the offset from Universal Time which tells me you should be using the offset, not the time zone abbreviation.

    You can also check out the other DateTimeStyles...


     

    Steve Wellens

    My blog
  • Re: Converting string to DateTime

    11-05-2009, 6:30 PM

    SGWellens:

    To skip the CET (which I assume is a time zone) you can do this:

    string Input = "Sun Feb 16 11:52:00 CET 2014";   
    1.   
    2. DateTime Output;   
    3.   
    4. if (DateTime.TryParseExact(Input, "ddd MMM dd hh:mm:ss \"CET\" yyyy"null, DateTimeStyles.None, out Output) == false)   
    5.     Output = DateTime.MinValue;  

    There is a format/parse character (K) to the offset from Universal Time which tells me you should be using the offset, not the time zone abbreviation.

    You can also check out the other DateTimeStyles...


     

     

    Brilliant!  I'd used ParseExact, but didn't think of \"CET\". 

  • Re: Converting string to DateTime

    11-06-2009, 2:09 AM
    • Member
      7 point Member
    • dreamzor
    • Member since 12-21-2006, 8:22 AM
    • Posts 78

    It does not work for me....

    I have tried all the DateTimeStyles also

  • Re: Converting string to DateTime

    11-06-2009, 10:09 AM
    • All-Star
      91,113 point All-Star
    • SGWellens
    • Member since 01-02-2007, 9:27 PM
    • Twin Cities, MN
    • Posts 7,430
    • Moderator
      TrustedFriends-MVPs

    If you cut and paste the example, it works.

    Saying something "does not work" is not going to solve any problems.  You need to provide more information. 

    Steve Wellens

    My blog
  • Re: Converting string to DateTime

    11-06-2009, 7:47 PM
    Answer
    • Contributor
      5,020 point Contributor
    • Paul Linton
    • Member since 04-29-2008, 11:16 PM
    • Posts 875

    Does 'CET' mean 'Central European Standard Time'?  If so you can convert the time to UTC like this (using Steve's base)

    string Input = "Sun Feb 16 11:52:00 CET 2014";
    
    DateTime CETDateTime=DateTime.MinValue;
    TimeZoneInfo cet = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
    
    DateTime.TryParseExact(Input, 
                                       "ddd MMM dd hh:mm:ss 'CET' yyyy", 
                                       null, 
                                       DateTimeStyles.None, 
                                       out CETDateTime);
    DateTime utcDateTime = TimeZoneInfo.ConvertTime(Output, 
                                                                               cet,
                                                                               TimeZoneInfo.Utc);
    
    // utcDateTime is now the DateTime in UTC and CETDateTime is the DateTime in CET (One hour ahead)


     

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
Page 1 of 1 (9 items)