服务器和客户端之间的日期时间差异
每个人! 我正在解决 ASP.NET MVC 项目中的下一个时间问题:
问题:需要计算客户端 DateTime 和服务器 DateTime 之间的差异。 我有 javascript 函数,它使用 timeStamp 参数 = getNow() 对服务器(DateController)进行 Ajax 查询,其代码如下。
getNow: function() {
var date = new Date();
return (date.getTime() + (date.getTimezoneOffset() * 60000));
}
接下来,在服务器端我有 DateController,其中我需要计算以毫秒为单位的日期时间差:
....
var clientMs = long.Parse(Request.QueryString["t"]);
var dt1970 = new DateTime(1970, 1, 1, 0, 0, 0);
var msFrom1970 = (DateTime.Now - dt1970).TotalMilliseconds
+(DateTime.UtcNow - DateTime.Now).TotalMilliseconds;
var timeOffset = msFrom1970 -clientMs;
return new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new { responseText = timeOffset.ToString()
};
}
但是 timeOffset 计算错误。我尝试在小型控制台应用程序中检查此逻辑:
class Program
{
static void Main(string[] args)
{
var clientMs = 1304411645875;
//value of clientMs I get from javaScript by using page
//http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_gettime
//with this script
/*
<HTML>
<body>
<script type="text/javascript">
var d=new Date();
document.write(d.getTime());
</script>
</body>
</HTML>
*/
var dt1970 = new DateTime(1970, 1, 1, 0, 0, 0);
var msFrom1970 = (DateTime.Now - dt1970).TotalMilliseconds
+(DateTime.UtcNow - DateTime.Now).TotalMilliseconds;
Console.WriteLine( msFrom1970);
Console.ReadLine();
}
}
差异约为几个小时。 有人可以告诉我我的错误或问题是由其他原因引起的吗?
everyone!
I'm solving next problem with time in my ASP.NET MVC project:
Problem: need to calculate difference between client DateTime and server DateTime.
I have javascript function, that do Ajax query to server (DateController) with timeStamp parameter = getNow(), code of which is below.
getNow: function() {
var date = new Date();
return (date.getTime() + (date.getTimezoneOffset() * 60000));
}
Next, on server side I have DateController, in which I need to calculate dateTime difference in milliseconds:
....
var clientMs = long.Parse(Request.QueryString["t"]);
var dt1970 = new DateTime(1970, 1, 1, 0, 0, 0);
var msFrom1970 = (DateTime.Now - dt1970).TotalMilliseconds
+(DateTime.UtcNow - DateTime.Now).TotalMilliseconds;
var timeOffset = msFrom1970 -clientMs;
return new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new { responseText = timeOffset.ToString()
};
}
But timeOffset is calculated wrong. I try to check this logic in small console app:
class Program
{
static void Main(string[] args)
{
var clientMs = 1304411645875;
//value of clientMs I get from javaScript by using page
//http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_gettime
//with this script
/*
<HTML>
<body>
<script type="text/javascript">
var d=new Date();
document.write(d.getTime());
</script>
</body>
</HTML>
*/
var dt1970 = new DateTime(1970, 1, 1, 0, 0, 0);
var msFrom1970 = (DateTime.Now - dt1970).TotalMilliseconds
+(DateTime.UtcNow - DateTime.Now).TotalMilliseconds;
Console.WriteLine( msFrom1970);
Console.ReadLine();
}
}
Difference is about hours.
Can someone show me my mistake or problem is occurred by something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您似乎在努力将 javascript 日期转换为 C# 日期。
[更新] getUTCMilliseconds() 只返回日期的毫秒部分 [/更新]
要将日期(以毫秒为单位)传递给服务器,请使用以下函数:并将 UTC 毫秒值转换为 .NET 中的日期时间:
您似乎缺少的是从毫秒到刻度的转换。
参考资料:
http://www.w3schools.com/jsref/jsref_getUTCMilliseconds.asp
http://twit88.com/blog/2011/01/ 23/net-datetime-from-milliseconds/
You seem to be struggling with converting a javascript date to a C# date.
[Update] getUTCMilliseconds() only returns the milliseconds part of a date [/Update]
To pass the date in milliseconds to the server, use this function:And to convert this value of UTC milliseconds to a DateTime in .NET:
What you seem to be missing is the conversion from milliseconds to ticks.
References:
http://www.w3schools.com/jsref/jsref_getUTCMilliseconds.asp
http://twit88.com/blog/2011/01/23/net-datetime-from-milliseconds/
即使对于给定的区域设置,夏令时也会阻止函数的 getTimezoneOffset() 返回值成为常量。
Daylight savings time prevents function's getTimezoneOffset() return from being a constant even for a given locale.
也许
DateTime.Now
大于DateTime.UtcNow
并且(DateTime.UtcNow - DateTime.Now).TotalMilliseconds
返回 0Maybe
DateTime.Now
greater thanDateTime.UtcNow
and(DateTime.UtcNow - DateTime.Now).TotalMilliseconds
returns 0