Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a strangely formatted DateTime. Anyone fancy stepping up?

I'm trying to parse a datestamp (that I got from Twitter) but am receiving errors. here's the datestamp:

Fri, 27 Aug 2010 22:00:07 +0000

Here's my code:

DateTime.ParseExact(MyDateValue, "ddd, dd MMM YYYY HH:mm:ss +ffff", new CultureInfo("en-US"))

and here's my error:

System.FormatException was unhandled Message=String was not recognized as a valid DateTime.

Anyone fancy taking that on? To make it easy I've provided the code below for a console app that exhibits the problem.

Thanks Jamie

using System;
using System.Globalization;

class Program
{
    static void Main(string[] args)
    {
        string MyDateValue = "Fri, 27 Aug 2010 22:00:07 +0000";
        var dt = DateTime.ParseExact(MyDateValue, "ddd, dd MMM YYYY HH:mm:ss +ffff", new CultureInfo("en-US"));

    }
}
like image 228
jamiet Avatar asked Feb 27 '23 08:02

jamiet


1 Answers

The year specifier is yyyy, not YYYY:

string MyDateValue = "Fri, 27 Aug 2010 22:00:07 +0000";
var dt = DateTime.ParseExact(MyDateValue, "ddd, dd MMM yyyy HH:mm:ss +ffff", new CultureInfo("en-US"));

The above works fine, as far as that it will not throw an exception.

I am assuming that the +0000 at the end of the string is supposed to be a timezone specifier. If so, the ffff is incorrect, as it stands for The hundred thousandths of a second, not the timezone specifier, which is K. If it is indeed supposed to be the timezone specifier, then this would be the correct code:

string MyDateValue = "Fri, 27 Aug 2010 22:00:07 +0000";
var dt = DateTime.ParseExact(MyDateValue, "ddd, dd MMM yyyy HH:mm:ss K", new CultureInfo("en-US"));

See Custom Date and Time Format Strings.

like image 130
Oded Avatar answered Feb 28 '23 22:02

Oded