将ISO日期字符串更改为Date对象-JavaScript [英] Change ISO Date String to Date Object - JavaScript

查看:177
本文介绍了将ISO日期字符串更改为Date对象-JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了一种怪异的境地,不幸的是,即使做了一些RnD和谷歌搜索之后,我也无法解决此问题。

I am stuck in a weird situation and unfortunately,even after doing some RnD and googling, i am unable to solve this problem.

我在其中有一个日期字符串ISO格式,例如 2014-11-03T19:38:34.203Z ,我想使用 new Date()将其转换为日期对象方法。

I have a date string in ISO format, like 2014-11-03T19:38:34.203Z and i want to convert it into a date object with new Date() method.

但是当我这样做时,输出为:

But when i do so, output is:

var isoDate = '2014-11-03T19:38:34.203Z';
console.log(new Date(isoDate)); //output is: Tue Nov 04 2014 01:08:34 GMT+0530 (IST)

The我通过的日期是 2014年11月3日,输出是 2014年11月4日,这是因为格林尼治标准时间+我们当地时间(IST)的5.30。

The date which i passed is of 3 Nov,2014 and the output is 4 Nov,2014 and it's because of GMT +5.30 of our local time(IST).

那么,有没有一种通用方法可以用来获取 date 对象,该对象返回 2014年11月3日

So, is there any generic method with which i can get the date object which return the date of Nov 3,2014.

注意:时间戳没有任何问题。我们可以使用 setHours()方法将时间字符串更改为零。我唯一想要的是 date 对象,例如 new Date(),日期为 3 2014年11月

NOTE: I don't have any issues with timestamp. We can change time string to zero with setHours() method. The only thing which i want is date object like new Date() having date of 3 Nov,2014.

推荐答案

不要将字符串传递给Date构造函数,众所周知,它不擅长于解析字符串。 IE 8完全不会解析ISO 8601格式的字符串,并返回 NaN 。编写自己的解析器非常简单:

Do not pass strings to the Date constructor, it is notoriously bad at parsing strings. IE 8, for one, will not parse ISO 8601 format strings at all and return NaN. It's really simple to write your own parser:

function parseISOString(s) {
  var b = s.split(/\D+/);
  return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5], b[6]));
}

请注意,如果时间是19:38:34.203 UTC和您的时区是UTC +0530,则该时区中的时间是第二天的01:08:34,因此日期有所不同。例如,对于一个在澳大利亚东海岸但没有遵守夏令时的人(即UTC +10),它相当于:

Note also that if the time is 19:38:34.203 UTC and your timezone is UTC +0530, then the time in that timezone is 01:08:34 am on the following day, hence the difference in dates. For example, for a person on the east coast of Australia but not observing daylight saving (i.e. UTC +10), it's equivalent to:

4 November, 2014 05:38:34



编辑



因此,如果要将其恢复为ISO日期,则可以使用 getISO * 方法来创建适合的格式,例如

Edit

So if you want to return it to an ISO date, you can use the getISO* methods to create whatever format that suits, e.g.

function isoFormatDMY(d) {  
  function pad(n) {return (n<10? '0' :  '') + n}
  return pad(d.getUTCDate()) + '/' + pad(d.getUTCMonth() + 1) + '/' + d.getUTCFullYear();
}

var s = '2014-11-03T19:38:34.203Z';
var date = parseISOString(s);

console.log(isoFormatDMY(date)) // 03/11/2014

或使用ES5的 toISOString

or use ES5's toISOString:

 parseISOString('2014-11-03T19:38:34.203Z').toISOString(); // 2014-11-03T19:38:34.203Z

适用于ES5之前版本浏览器的简单polyfill:

A simple polyfill for pre ES5 browsers:

if (!Date.prototype.toISOString) {

  Date.prototype.toISOString = function() {

    var d = this;

    // Padding functions 
    function pad(n) {return (n<10? '0' :  '') + n}
    function padd(n){return (n<100? '0' : '') + pad(n)}

    return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) +
           'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + 
           pad(d.getUTCSeconds()) + '.' + padd(d.getMilliseconds()) + 'Z';
  }
}

这篇关于将ISO日期字符串更改为Date对象-JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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