Chrome和Firefox中的Javascript日期不正确 [英] Incorrect Javascript Date in Chrome vs Firefox

查看:163
本文介绍了Chrome和Firefox中的Javascript日期不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Chrome中得到了不正确的日期...

我的代码看起来像这样..

标题包含2013-06-14T00:00:00,它是从WebAPI返回的C#中的DateTime

正如你可以在这里看到的两个浏览器..



当我添加到一个新的JavaScript日期,像这样..
var dt = new Date(title)



我在不同的浏览器中得到不同的日期...



示例 - http://jsfiddle.net/RvUSq/

解决方案

将时间戳转换为 ISO 8601 格式化字符串,例如

  var title =2013年6月14日00: 00:00//从C#
打印


然后使用 Date 构造函数

  var date = new Date(title); 

如果您不指定时区,则客户机中的本地时区将设置为给定时间。如果您指定时区,则需要进行计算以将日期转换为当地时区。

  var title =2013年6月14日00:00:00\" ; 
var date = new Date(title); // Fri Jun 14 2013 00:00:00 GMT + 0530(IST)

var title =2013年6月14日00:00:00 GMT;
var date = new Date(title); //星期五2013年6月14日05:30:00 GMT + 0530(IST)

var title =2013年6月14日00:00:00 GMT-0400;
var date = new Date(title); // Fri Jun 14 2013 09:30:00 GMT + 0530(IST)

ref: https://developer.mozilla.org/zh-CN / docs / Web / JavaScript / Reference / Global_Objects / Date / parse

I'm getting incorrect dates in Chrome...

My code looks like this..

Title contains "2013-06-14T00:00:00", it was a DateTime in C# returned from WebAPI

As you can see here on both browsers..

When I add it to a new javascript date like this.. var dt = new Date(title)

I get different dates in different browsers...

Example - http://jsfiddle.net/RvUSq/

解决方案

Convert timestamp to ISO 8601 formatted string in C#, for e.g

var title = "14 JUN 2013 00:00:00" // printed from C#

Then use Date constructor

var date = new Date(title);

If you don't specify timezone the local timezone in the client machine will be set to the given time. If you specify the timezone, needed calculations will be done to convert the date to local timezone.

var title = "14 JUN 2013 00:00:00";
var date = new Date(title); // Fri Jun 14 2013 00:00:00 GMT+0530 (IST)

var title = "14 JUN 2013 00:00:00 GMT";
var date = new Date(title); // Fri Jun 14 2013 05:30:00 GMT+0530 (IST)

var title = "14 JUN 2013 00:00:00 GMT-0400";
var date = new Date(title); // Fri Jun 14 2013 09:30:00 GMT+0530 (IST)

ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

这篇关于Chrome和Firefox中的Javascript日期不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆