Date.parse()在Fire Fox和IE中不工作。它可以在Chrome中正常工作 [英] Date.parse() is not working in Fire Fox and IE.Its working fine in Chrome

查看:115
本文介绍了Date.parse()在Fire Fox和IE中不工作。它可以在Chrome中正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将具有日期时间的字符串转换为日期时间格式。
我的代码是:

 在Chrome中它的工作正常:

var str = 05-Sep-2013 01:05:15 PM
var res = Date.parse(str)
console.log(res)// o / p:1378366515000
var result = new Date(res)
console.log(result)// o / p:Thu Sep 05 2013 13:05:15 GMT + 0530(印度标准时间)

在Firefox和IE中:
console.log(res)// o / p:NaN
console.log(result)// o / p:Date {Invalid Date}
/ pre>

你能帮助我吗

解决方案

根据

中的建议自己解析字符串

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


不建议将Date.parse作为直到ES5,解析
字符串是完全依赖于实现的。在不同的主机解析日期字符串之间仍然有许多
的差异,因此日期
字符串应该手动解析(如果要容纳许多
个不同的格式,库可以帮助)。


我给了你一个关于SO的答案的链接,解释了如何做到这一点。



将字符串转换为JS中的日期格式



这个例子应该是工作,即使是非常老的或非常破碎的浏览器。



  var lookupMonthName = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,jul:6,aug:7,sep: 8,oct:9,nov:10,dec:11}; function customParse(dateTimeStr){var dateTime = dateTimeStr.replace(/ ^ [\ s \\\ \xA0] + | [\s\\\\xA0] + $ / g,'').split(''); var date = dateTime [0] .split(' - '); date [1] = lookupMonthName [date [1] .toLowerCase()]。toString(); date.reverse(); var time = dateTime [1] .split(':'); if(dateTime [2] .toUpperCase()==='PM'){time [0] =(parseInt(time [0],10)+ 12).toString() } var args = date.concat(time);的console.log(参数);返回新日期(Date.UTC.apply(null,args));} var str = '05 -Sep-2013 01:05:15 PM'; var date = customParse(str); document.getElementById('out') .appendChild(document.createTextNode(date)); console.log(date);  

 < pre id =out>< / pre>  



要从Date对象格式化字符串,请参阅SO答案



在哪里可以找到关于在JavaScript中格式化日期的文档?



您有一点努力,您可以自己找到这些信息。


How to convert the string which is having date time to date time format. My code is:

      In Chrome its working fine:

      var str = "05-Sep-2013 01:05:15 PM " 
      var res = Date.parse(str) 
      console.log(res) //o/p:1378366515000
      var result = new Date(res)
      console.log(result) //o/p:Thu Sep 05 2013 13:05:15 GMT+0530 (India Standard Time)

     In Firefox and IE:
     console.log(res) //o/p: NaN
     console.log(result) //o/p: Date {Invalid Date}

Could you please help me out. thanks in advance.

解决方案

Parse the string yourself as suggested on

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

It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).

I gave you a link to an answer on SO that explains how to do this.

Converting String into date format in JS

This example should work even on very old or very broken browsers.

var lookupMonthName = {
  jan: 0,
  feb: 1,
  mar: 2,
  apr: 3,
  may: 4,
  jun: 5,
  jul: 6,
  aug: 7,
  sep: 8,
  oct: 9,
  nov: 10,
  dec: 11
};

function customParse(dateTimeStr) {
  var dateTime = dateTimeStr.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '').split(' ');
  var date = dateTime[0].split('-');
  date[1] = lookupMonthName[date[1].toLowerCase()].toString();
  date.reverse();
  var time = dateTime[1].split(':');
  if (dateTime[2].toUpperCase() === 'PM') {
    time[0] = (parseInt(time[0], 10) + 12).toString();
  }
  var args = date.concat(time);
  console.log(args);
  return new Date(Date.UTC.apply(null, args));
}

var str = '05-Sep-2013 01:05:15 PM ';
var date = customParse(str);
document.getElementById('out').appendChild(document.createTextNode(date));
console.log(date);

<pre id="out"></pre>

To format a string from a Date object, see SO answers

Where can I find documentation on formatting a date in JavaScript?

A little effort on your part and you would have been able to find this information yourself.

这篇关于Date.parse()在Fire Fox和IE中不工作。它可以在Chrome中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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