I convert javascript date to C# DateTime.
When I use firefox, JavaScript return date to my C# function:
string jsDate = "Fri Dec 05 2014 00:00:00 GMT+0100";
so, I parse it to C# DateTime using:
DateTime.TryParseExact(JsDate, "ddd MMM dd yyyy HH:mm:ss 'GMT'K", CultureInfo.InvariantCulture, DateTimeStyles.None, out Date)
When I using Chrome, js return me date in format: "Fri Dec 05 2014 00:00:00 GMT+0100 (Ćrodkowoeuropejski czas stand.)"
(
Central European standard time)
How I can parse the second one time?
Basically, you shouldn't use the default string representation from the browser. Otherwise you need to know which language it's going to use for month names etc, and you're basically fighting a losing battle.
I would strongly recommend that you format the string in a culture-neutral way when you pass it from the browser to the server - e.g. as ISO-8601 such as yyyy-MM-ddTHH:mm:ssZ
. You should consider whether or not you need the time zone offset - you may just want to send it in UTC for simplicity. (If you do send the offset from UTC, you should probably parse it as a DateTimeOffset
in your C# code.)
See more on this question at Stackoverflow