将 JavaScript 日期转换为 .NET 日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10699851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert JavaScript Date to .NET DateTime
提问by Christian Agius
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:
我从 JavaScript 获取一个 Date 值到 MVC 中的控制器,我想将它解析为 .NET 格式 DateTime 但它给了我一个错误,例如:
The string was not recognized as a valid DateTime.
该字符串未被识别为有效的 DateTime。
The Format of the JavaScript date is:
JavaScript 日期的格式为:
"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!
任何人都可以给我一个示例代码吗?谢谢!
采纳答案by Jon
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
:
不是解析文本表示,DateTime
而是从时间戳构造 a 会更健壮。从 JS 获取时间戳Date
:
var msec = date.getTime();
And to convert msec
(which represents a quantity of milliseconds) into a DateTime
:
并将msec
(表示毫秒数)转换为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
回答by Martin ?hlin
The following parses well with the default DateTime modelbinder in a .NET MVC Controller:
以下使用 .NET MVC 控制器中的默认 DateTime 模型绑定器解析得很好:
var myJsDate = new Date();
var myDotNetDate = myJsDate.toISOString();
回答by The BitMaster
Here is what I did and why. I hope this Helps.
这是我所做的以及为什么。我希望这有帮助。
JS Date var d = new Date()
JS日期 var d = new Date()
returns: Thu Nov 19 08:30:18 PST 2015
返回:太平洋标准时间 2015 年 11 月 19 日星期四 08:30:18
C# does not like this format so convert it to UTC:
C# 不喜欢这种格式,因此将其转换为 UTC:
var dc = d.toUTCString()
returns: Thu, 19 Nov 2015 16:30:18 UTC
返回:UTC 时间 2015 年 11 月 19 日星期四 16:30:18
UTC – The Worlds Time Standard is not a time zone so you need to change it to a time zone
UTC – 世界时间标准不是时区,因此您需要将其更改为时区
var cr = dc.replace("UTC","GMT")
now it is ready to
现在它准备好了
Thu, 19 Nov 2015 16:30:18 GMT
2015 年 11 月 19 日星期四 16:30:18 GMT
In one line
在一行
var ol = d.toUTCString().replace("UTC","GMT")`
Thu, 19 Nov 2015 16:30:18 GMT
2015 年 11 月 19 日星期四 16:30:18 GMT
for C#
对于 C#
DateTime DateCreated= DateTime.Parse(ol);
回答by Marco Lackovic
You don't need any conversion: the default DateTime modelbinder in a .NET MVC Controller works fine with a JavaScript Date object.
您不需要任何转换:.NET MVC 控制器中的默认 DateTime 模型绑定器适用于 JavaScript 日期对象。
Using Moment.js
1) .NET DateTime -> JavaScript Date
1) .NET 日期时间 -> JavaScript 日期
var jsDate = moment(dotNetDateTime).toDate();
2) JavaScript Date -> .NET DateTime
2) JavaScript 日期 -> .NET 日期时间
var dotNetDateTime = jsDate;