通过 Ajax 将 Javascript 日期转换为 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1877788/
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
Javascript date to C# via Ajax
提问by Ian Vink
I have javascript date object which gives me a date string in this format, "Wed Dec 16 00:00:00 UTC-0400 2009".
我有一个 javascript 日期对象,它以这种格式为我提供了一个日期字符串,“Wed Dec 16 00:00:00 UTC-0400 2009”。
I pass this via Ajax to the server (ASP.NET c#)
我通过 Ajax 将它传递给服务器(ASP.NET c#)
How can I convert, "Wed Dec 16 00:00:00 UTC-0400 2009" to a C# DateTime object. DateTime.Parse fails.
如何将“Wed Dec 16 00:00:00 UTC-0400 2009”转换为 C# DateTime 对象。DateTime.Parse 失败。
采纳答案by dtb
You can use DateTime.ParseExactwhich allows you to specify a format string to be used for parsing:
您可以使用DateTime.ParseExact,它允许您指定用于解析的格式字符串:
DateTime dt = DateTime.ParseExact("Wed Dec 16 00:00:00 UTC-0400 2009",
"ddd MMM d HH:mm:ss UTCzzzzz yyyy",
CultureInfo.InvariantCulture);
回答by Jay
To be honest I wouldn't try to parse that date string in C#, I'd personally try to create a more useful date structure from your javascript date object.
老实说,我不会尝试在 C# 中解析该日期字符串,我个人会尝试从您的 javascript 日期对象创建一个更有用的日期结构。
For instance you could use parse()
in javascript which will return the ms representing the date object, which you can use DateTime.Parse()
on to convert into a C# DateTime object.
例如,您可以parse()
在 javascript 中使用它将返回表示日期对象的 ms,您可以使用DateTime.Parse()
它来转换为 C# DateTime 对象。
回答by EMP
The most reliable way would be to use milliseconds since the epoch. You can easily get this in JavaScript by calling Date.getTime()
. Then, in C# you can convert it to a DateTime like this:
最可靠的方法是使用自纪元以来的毫秒数。您可以通过在 JavaScript 中调用Date.getTime()
. 然后,在 C# 中,您可以将其转换为 DateTime,如下所示:
long msSinceEpoch = 1260402952906; // Value from Date.getTime() in JavaScript
return new DateTime(1970, 1, 1).AddTicks(msSinceEpoch * 10000);
You have to multiply by 10,000 to convert from milliseconds to "ticks", which are 100 nanoseconds.
您必须乘以 10,000 才能将毫秒转换为“滴答”,即 100 纳秒。
回答by RJB
Just for posterity, to help future fellow Googlers, I'd like to expand on EMP's answer.
只是为了后代,为了帮助未来的 Google 员工,我想扩展 EMP 的答案。
EMP's answer provides the time in UTC(if that's what you're looking for, use that).
EMP 的答案提供了UTC 时间(如果这是您要查找的时间,请使用它)。
To arrive at the client local timein C#:
在 C# 中到达客户端本地时间:
In JavaScript:
在 JavaScript 中:
var now = new Date();
var UTC = now.getTime();
var localOffset = (-1) * now.getTimezoneOffset() * 60000;
var currentTime = Math.round(new Date(UTC + localOffset).getTime());
In C#:
在 C# 中:
DateTime currentTimeDotNet = new DateTime(1970, 1, 1).AddTicks(Convert.ToInt64(currentTime) * 10000);
Credit to this blogand EMP's answer, but took some trial and error on both ends to get it right, so just fyi for future folks.
归功于此博客和 EMP 的回答,但在两端都进行了一些试验和错误以使其正确,因此仅供未来的人们参考。
回答by Hovis Biddle
This may not be possible in your case, but I really recommend updating the JS code to pass dates/times in ISO 8601 format. http://en.wikipedia.org/wiki/ISO_8601
这在您的情况下可能是不可能的,但我真的建议更新 JS 代码以传递 ISO 8601 格式的日期/时间。 http://en.wikipedia.org/wiki/ISO_8601
ISO 8601 is not only the formal standard, it's also easy to use and prevents a lot of timezone hassle!
ISO 8601 不仅是正式的标准,它也易于使用,避免了很多时区的麻烦!
To get 8601 datetime strings in Javascript:
要在 Javascript 中获取 8601 个日期时间字符串:
var d = new Date();
var iso_time = d.toISOString(); //"2014-05-06T18:49:16.029Z"
To read 8601 datetime strings in C#:
在 C# 中读取 8601 日期时间字符串:
DateTime d = DateTime.Parse(json_string);