Convert JavaScript Date to .NET DateTime
.net, date, datetime, javascript
Solution
Instead of parsing a textual representation it would be more robust to construct a `DateTime` from a timestamp instead. To get a timestamp from a JS `Date`:
var msec = date.getTime();
And to convert `msec` (which represents a quantity of milliseconds) into a `DateTime`:
var date = new DateTime(1970, 1, 1, 0, 0, 0, 0); // epoch start
date = date.AddMilliseconds(msec); // you have to get this from JS of course
Problem
I getting a Date value from JavaScript to a controller in MVC and I would like to parse it to .NET format DateTime but its giving me an error such as: The string was not recognized as a valid DateTime. The Format of the JavaScript date is: ``` "Wed May 23 2012 01:40:00 GMT+0200 (W. Europe Daylight Time)" ``` I've tried this but its not working: ``` DateTime.ParseExact(begin.Substring(1, 24), "ddd MMM d yyyy HH:mm:ss", CultureInfo.InvariantCulture); ``` anyone can give me a sample code please? thanks!