转换的JavaScript日期为.NET日期时间日期、时间、JavaScript、NET

2023-09-05 04:41:12 作者:警告系统出错!!!

我得到在MVC控制器通过JavaScript Date值,我想将其解析到.NET格式,日期时间,但它给我的错误,如:

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:

该字符串未被识别为有效的DateTime。

The string was not recognized as a valid DateTime.

JavaScript的日期格式为:

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

任何人都可以给我一个样品code吗?谢谢!

anyone can give me a sample code please? thanks!

推荐答案

而不是解析文本重新presentation这将是更强大的构建的DateTime 从时间戳来代替。要从JS 日期得到一个时间戳:

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();

和转换毫秒(其中重presents毫秒的数量)成的DateTime

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