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.