0

I have this string: "14. januar 2013 9:15:00 EST".

From what I understand the format is the following: d. MMMM yyyy H:mm:ss EST, but I've tried the following code, and nothing works. What am I doing wrong here?

    private static string[] _dateFormats = new[] {
            @"d. MMMM yyyy H:mm:ss \E\S\T",
            @"d. MMMM yyyy h:mm:ss \E\S\T",
            @"d. MMMM yyyy H:mm:ss E\S\T",
            @"d. MMMM yyyy h:mm:ss E\S\T",
            @"d. MMMM yyyy H:mm:ss EST",
            @"d. MMMM yyyy h:mm:ss EST",
            @"dd-MM-yyyy HH:mm:ss"
        };

    private static DateTimeStyles _styles = DateTimeStyles.AllowLeadingWhite
        | DateTimeStyles.AllowInnerWhite
        | DateTimeStyles.AllowTrailingWhite
        | DateTimeStyles.AllowWhiteSpaces;

    DateTime dt;
    DateTime.TryParseExact(dateTime, _dateFormats, CultureInfo.InvariantCulture, _styles, out dt);

Update:

Using Danish CultureInfo did the trick. However this doesn't work either: Tir, 15 Jan 2013 11:00:00

DateTime.TryParseExact(test, @"ddd, dd MMM yyyy HH:mm:ss", CultureInfo.GetCultureInfo("da-DK"), _styles, out dt);

Tir is equal to Tue or "Tuesday". Why isn't it recognized?

Basically I'm trying to parse loads of somewhat malformed datetime strings used to monitor RSS feeds. Many RSS feeds does not validate, but as I cannot change this, I need a good method for parsing them anyway. I have though about using the current time as a sort of guideline, as the feed item parsed, shouldn't be much older than one hour. This means that the lack of correct timezone, could be set to the latest minute in the previous hour.

Eg. I get the following datetime string: Tir, 15 Jan 2013 11:15:00. It has no indication of timezone, but as the feed item is extracted at 14:18 UTC, it is most likely published 14:15 UTC.

This question started as a somewhat trivial bug fix, but is more likely a question about how to approach this. Does anyone have some good ideas?

Thanks.

  • what output are you getting? – kolin Jan 15 '13 at 10:53
  • 1) What are you trying to do? 2) What is wrong about it? Are you getting an error? – musefan Jan 15 '13 at 10:53
  • If you remove "EST" from the string and your format strings, does it work? – Jake Hendy Jan 15 '13 at 10:53
  • 1
    In `InvariantCulture` the name of the month is `January`, not `Januar`. – AakashM Jan 15 '13 at 10:55
  • TryParseExact returns false. januar i January in Danish. Removing EST or using zz instead doesn't change anything. – madebysoren Jan 15 '13 at 11:01
  • Works: DateTime.TryParseExact(@"14. January 2013", "d. MMMM yyyy", CultureInfo.InvariantCulture, _styles, out dt); Doesn't work: DateTime.TryParseExact(@"14. januar 2013", "d. MMMM yyyy", CultureInfo.InvariantCulture, _styles, out dt); I thought InvariantCulture was independent? – madebysoren Jan 15 '13 at 11:10

1 Answers1

1

You are going to have to get rid of the "EST" timezone indicator. They are not compatible and grossly ambiguous. EST can be Eastern Standard Time both in the USA (UTC-5) and Australia (UTC+10). CST is a doozy with 5 distinct timezones. You'll have to use String.SubString() to lop it off.

The string is otherwise in a standard format for Danish dates, ParseExact() is not required. This worked well on my en-US machine:

var ci = System.Globalization.CultureInfo.GetCultureInfo("da-DK");
var s = "14. januar 2013 9:15:00";
var dt = DateTime.Parse(s, ci.DateTimeFormat);
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • That is some really good points. I've updated my question a bit. Your approach works, but doesn't work with this format: Tir, 15 Jan 2013 11:00:00 – madebysoren Jan 15 '13 at 13:12
  • How many more do you have? Start another question. Fwiw, "Tir" is not an appropriate abbreviation for Tuesday in Danish, it is "Ti". The ParseExact format string is `ddd, dd MMM yyyy HH:mm:ss` – Hans Passant Jan 15 '13 at 13:26
  • Well. I'm not sure. Every time I add a new RSS feed, I can potentially have one more, but luckily that's not the case. Well that's the format I was trying to match on, but as they use Tir instead of Ti, then it seems like I just cannot do it like this. Maybe it is just easier to contact the websites with malformed RSS feeds, and ask them to fix it. – madebysoren Jan 15 '13 at 17:07