【问题标题】:Javascript date issue on IE 7IE 7 上的 Javascript 日期问题
【发布时间】:2013-10-07 14:35:28
【问题描述】:

我有一个 Web 方法,我从该方法返回 Date 它正在返回日期,例如 "Mon Sep 30 07:26:14 EDT 2013",当我在我的 javascript 代码中转换日期格式时:

var d= SomeDate.format("MM/dd/yyyy hh:mm:ss tt"); //Somedate is comming from web method 

但在 IE7(09/30/2013 04:56:14 PM) 中显示错误的时间,但在 IE9 (09/30/2013 07:26:14 AM)中工作正常。

我们如何在 IE7 中做到这一点?

【问题讨论】:

  • 你能检查一下 IE7 和 IE9 的时间戳是否一致吗?做SomeDate.getTime(),这应该给你相同的号码。

标签: javascript asp.net json webmethod


【解决方案1】:

Date.format 不适用于 IE7。 您可以像这样使用 Date 类:

var currentDate = new Date();
var month = currentDate.getMonth();
var day = currentDate.getDay();
month = (month < 10) ? '0' + month : month;
day = (day < 10) ? '0' + day : day;
var formatedDate = month + "/" + day + "/" + currentDate.getFullYear() + " " + currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
alert (formatedDate);

例如:http://jsbin.com/enAqohO/1/edit?html,js,output

【讨论】:

  • 我们如何将其转换为 12 小时时钟
【解决方案2】:

看起来它与时区有关。 IE7 无法识别字符串中的时区“EDT”。

我将尝试将“EDT”移动到字符串的末尾。可以试试吗

new Date("Mon Sep 30 07:26:14 2013 EDT") 

看看它是否给你正确的结果?抱歉,我没有 IE7 可以测试。

【讨论】:

  • 如何在代码中将 EDT 移动到最后,我从 webMethod 获得的数据作为返回,我必须添加单独的逻辑才能将 EDT 移动到最后
猜你喜欢
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 2012-06-30
  • 2014-03-05
  • 2011-03-21
  • 2016-10-22
  • 1970-01-01
相关资源
最近更新 更多