3

How Can I convert following date string to dateTime:

Fri, 18 Dec 2009 9:38 am PST

I tried DateTime.Parse(string)

I got following error:

The string was not recognized as a valid DateTime. There is an unknown word starting at index 25. System.SystemException {System.FormatException}

UPDATE

I tried to get weather from yahoo and I tried to get date like this:

Date = DateTime.Parse(feed.Element(yWeatherNS + "condition").Attribute("date").Value),

I debugged it. date attribute is correct (like above).

Thanks.

AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
  • 1
    Have you tried [`DateTime.Parse`](http://msdn.microsoft.com/en-us/library/1k1skd40.aspx) or [`DateTime.ParseExact`](http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx)? – huysentruitw Oct 19 '12 at 06:27

2 Answers2

7

I don't think there's anything in the BCL which will parse time zone abbreviations. (They should be avoided where possible anyway, as they can be ambiguous.)

If you don't mind losing the time zone information, you can use something like this:

using System;
using System.Globalization;

static class Test
{
    static void Main()
    {
        string text = "Fri, 18 Dec 2009 9:38 am PST";
        DateTime parsed = TrimZoneAndParse(text);
        Console.WriteLine(parsed);
    }

    static DateTime TrimZoneAndParse(string text)
    {
        int lastSpace = text.LastIndexOf(' ');
        if (lastSpace != -1)
        {
            text = text.Substring(0, lastSpace);
        }
        return DateTime.ParseExact(text,
            "ddd, dd MMM yyyy h:mm tt",
            CultureInfo.InvariantCulture);
    }
}

Note that that assumes a fixed date/time format and culture. Your needs may vary, and you should also consider using TryParse or TryParseExact if this is user input.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

If DateTime.Parse can't figure it out automatically, you can use DateTime.ParseExact where you specify the format being used.

In your case this would be something like, you'll need to replace the 'PST' yourself however:

CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = "Fri, 18 Dec 2009 9:38 am PST";
dateString = dateString.Replace("PST", "-08:00");
string format = "ddd, dd MMM yyyy h:mm tt zzz";
DateTime result = DateTime.ParseExact(dateString, format, provider);

If your program needs to work with different timezone abbrevations, you'll have to build a Dictionary with abbrevation to time zone offset conversions.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
  • ... except that `zzz` doesn't recognize time zone abbreviations, only UTC offsets as far as I'm aware. – Jon Skeet Oct 19 '12 at 06:37
  • It's better to use `DateTimeOffset` rather than `DateTime` as this preserves the time zone info. – Enigmativity Oct 19 '12 at 06:39
  • @JonSkeet: idd, figured that out quickly ;) – huysentruitw Oct 19 '12 at 06:40
  • @Enigmativity: No, `DateTimeOffset` preserves the offset from UTC. That's not the same thing as a time zone. (.NET doesn't have any type to represent "date and time in a specific time zone") – Jon Skeet Oct 19 '12 at 06:44
  • @WouterH: PST is actually UTC-8, not UTC-7... and unless you're suggesting a similar replacement for *all* time zone identifiers, it's not really a general purpose approach IMO. – Jon Skeet Oct 19 '12 at 06:45
  • @JonSkeet: if RFC822 was supported by .NET, having a replacement for all identifiers is exaclty what would happen under the hood. So why not implement it yourself? In fact you can extract the information from the Windows registry, see: `HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones`. I think you can create the abbrevations yourself from the `Std` value and extract the offset from the `Display` value – huysentruitw Oct 19 '12 at 06:53
  • @JonSkeet - I stand corrected. I did mean UTC offset when I said time zone information, but you're perfectly correct that it could be misconstrued. – Enigmativity Oct 19 '12 at 07:08