Parse Javascript Date for C # DateTime
I have a date object in JavaScript that gives me "Wed Oct 01 2014 00:00:00 GMT+0200"
:;
I'm trying to parse it, but I get an exception:
string Date = "Wed Oct 01 2014 00:00:00 GMT+0200";
DateTiem d = DateTime.ParseExact(Date,
"ddd MM dd yyyy HH:mm:ss GMTzzzzz",
CultureInfo.InvariantCulture);
source to share
MM
format specifier - 2
month number from 01
to 12
.
Instead of the abbreviated month name, you need to use MMM
the format specifier .
And for your part, +0200
you need to use K
a format specifier that has timezone information, not zzzzz
.
And you need to use single quotes for your GMT
part as 'GMT'
to specify it as a literal string separator.
string s = "Wed Oct 01 2014 00:00:00 GMT+0200";
DateTime dt;
if(DateTime.TryParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'K",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
Any z
format specifier is discouraged by parsing DateTime
. Because they represent the signed offset of the local UTC timezone value, this specifier has no effect on DateTime.Kind
. And DateTime
does not store offset values.
This is why this specifier comes up instead of DateTimeOffset
.
source to share