0

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?

user1031034
  • 836
  • 1
  • 14
  • 38
  • With `TryParseExact`, you have a `DateTime` at the end, not a `string`. That means, you have already a `DateTime` based on your `"Fri Dec 05 2014 00:00:00 GMT+0100"` string. There is no need additional parsing. I don't know what is `Środkowoeuropejski czas stand` but it seems just a additional representation to string representation of your `DateTime`. – Soner Gönül Dec 05 '14 at 09:30
  • @SonerGönül: I think the point is that the input that Chrome gives is inappropriate for the code - it's not a very clearly written question. – Jon Skeet Dec 05 '14 at 09:32
  • 1
    Alternatively, you can try to use milliseconds/seconds as the common base, see: http://stackoverflow.com/a/1877827/265165 – thmshd Dec 05 '14 at 09:34
  • @JonSkeet Yeah, sound like that. Not clear question of course, but clearly OP try to using inappropriate string representation of Chrome. – Soner Gönül Dec 05 '14 at 09:39

2 Answers2

3

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0
String dateString = "Fri Dec 05 2014 00:00:00 GMT+0100 (Środkowoeuropejski czas stand.)"
dateString = dateString.subStr(0,dateString.indexOf('(')-1);
DateTime.TryParseExact(dateString, "ddd MMM dd yyyy HH:mm:ss 'GMT'K", CultureInfo.InvariantCulture, DateTimeStyles.None, out Date)
JSK NS
  • 3,346
  • 2
  • 25
  • 42