Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  94] [ 5]  / answers: 1 / hits: 20068  / 12 Years ago, tue, may 22, 2012, 12:00:00

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!


More From » .net

 Answers
24

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

[#85427] Monday, May 21, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cruzs

Total Points: 710
Total Questions: 113
Total Answers: 100

Location: Nepal
Member since Sat, Jul 18, 2020
4 Years ago
cruzs questions
Thu, Nov 26, 20, 00:00, 4 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Wed, Aug 19, 20, 00:00, 4 Years ago
Sun, Aug 2, 20, 00:00, 4 Years ago
;