从没有时区的日期获取ISO字符串 [英] Get ISO string from a date without time zone

查看:105
本文介绍了从没有时区的日期获取ISO字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的配置是里斯本时区。当我执行 new Date()时,我得到的当前本地日期/时间是 Fri Apr 28 2017 01:10:55 GMT + 0100(GMT白天时间)

My configuration is Lisbon time zone. When I do new Date() I get my current local date/time which is Fri Apr 28 2017 01:10:55 GMT+0100 (GMT Daylight Time)

当我使用 toISOString()获得ISO字符串时,它将应用时区我会得到:

When I get the ISO string with toISOString() it will apply the time zone and I will get:

2017-04-28T00:10:55.964Z

问题在于几分钟前的日期时间是这样(昨天):

The problem is that some minutes ago the date time was like this (yesterday):

2017-04-27T23:45:05.654Z

我尝试了moment.js(对此是新手),并且我做了类似的事情

I tried moment.js (new to this) and I did something like this

document.write(moment('2017-04-28').format())

但是我得到这个 2017-04-28T00:00:00 + 01:00 而不是 2017-04-28T00:00:00.000Z

But I get this 2017-04-28T00:00:00+01:00 which is not 2017-04-28T00:00:00.000Z

我想将此值作为重载方法的参数传递,以自动将其解析为DateTime类型,但如果我通过moment.js格式的输出,则它将被解析为 2017-04-27 23:00:00.00

I want to pass this value as a parameter of a restful method to parse it automatically as a DateTime type, but if I pass the output from moment.js format then it will get parsed as 2017-04-27 23:00:00.00

如果我创建一个 new Date() new Date('2017-04-27')的新日期(日期部分) ,我只想获取无时区的ISO格式,如下所示:

If I create a new date with new Date() or with new Date('2017-04-27') (date portion), I just want to get the ISO format like as follows with no time zone:

2017-04-28T00:00:00.000Z

是否有toISOString()之类的javascript方法,或者可能需要花点时间来获取该格式?

Is there a javascript method like toISOString() or using maybe moment to get that format?

无论是什么时区或一天中的什么时刻,我都只是模拟那是给定日期的午夜。

No matter what time zone or moment of day, I just to simulate that is midnight of the date given.

推荐答案

不清楚你在问什么。如果您希望UTC日期的小时数始终为0,则将UTC时间设置为0并使用 toISOString ,例如

It's very unclear what you're asking. If you want the UTC date with the hours always 0, then set the UTC hours to 0 and use toISOString, e.g.

var d = new Date();
d.setUTCHours(0,0,0,0);
console.log(d.toISOString());

当然,这将显示UTC日期,可能有所不同

Of course this is going to show the UTC date, which may be different to the date on the system that generated the Date.

此外,

new Date('2017-04-27').toISOString();

应该返回2017-04-27T00:00:00Z(即应该根据ECMA-262解析为UTC,这与将其视为本地的ISO 8601相反),但是在所有使用的实现中都不可靠。

should return 2017-04-27T00:00:00Z (i.e. it should be parsed as UTC according to ECMA-262, which is contrary to ISO 8601 which would treat it as local), however that is not reliable in all implementations in use.

如果您只想获取ISO 8601格式的当前日期,则可以执行以下操作:

If you just want to get the current date in ISO 8601 format, you can do:

if (!Date.prototype.toISODate) {
  Date.prototype.toISODate = function() {
    return this.getFullYear() + '-' +
           ('0'+ (this.getMonth()+1)).slice(-2) + '-' +
           ('0'+ this.getDate()).slice(-2);
  }
}

console.log(new Date().toISODate());

但是,由于内置的​​ toISOString 使用UTC,这可能会造成混淆。如果需要UTC日期,则:

However, since the built-in toISOString uses UTC this might be confusing. If the UTC date is required, then:

if (!Date.prototype.toUTCDate) {
  Date.prototype.toUTCDate = function() {
    return this.getUTCFullYear() + '-' +
           ('0'+ (this.getUTCMonth()+1)).slice(-2) + '-' +
           ('0'+ this.getUTCDate()).slice(-2);
  }
}

console.log(new Date().toUTCDate());

这篇关于从没有时区的日期获取ISO字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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