最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Incorrect Javascript Date in Chrome vs Firefox - Stack Overflow

programmeradmin1浏览0评论

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 - /

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/

Share Improve this question edited Jun 27, 2013 at 5:06 jaekie asked Jun 27, 2013 at 4:33 jaekiejaekie 2,3034 gold badges31 silver badges54 bronze badges 1
  • what is the title there – Ganesh Rengarajan Commented Jun 27, 2013 at 4:38
Add a comment  | 

2 Answers 2

Reset to default 14

Looks like Firefox is assuming this datetime format without timezone is local time and Chrome/Webkit is assuming it's UTC.

If the datetime returned from the api is UTC, simply append a "Z" to the end of the string, so it becomes "2013-06-14T00:00:00Z", which indicates the time is in UTC, then you will get the same result in the two browsers.

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

发布评论

评论列表(0)

  1. 暂无评论