在javascript中获取UTC日期(不是UTC字符串) [英] get UTC date (not UTC string) in javascript

查看:74
本文介绍了在javascript中获取UTC日期(不是UTC字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用时刻或任何其他可能性来获取 UTC 日期.utcString = moment.utc(someDate)).format() 给出的是 utc 字符串而不是 utc 日期.我无法使用它执行诸如 utcString.getDate() 等操作.请帮助我提供 UTC 日期对象.

I want to get utc date using moment or any other possibilities. utcString = moment.utc(someDate)).format() is giving utc string not utc date. I am not able to perform operations like utcString.getDate() , etc using this. Please help me in providing with utc date object.

moment.utc(new Date()).toDate() 再次将日期转换为我的当地时间.

moment.utc(new Date()).toDate() is converting date again to my local time.

例如:我希望像 "2019-10-31T00:00:00.000Z" 这样的 utc 格式的字符串转换为 utc 日期并执行操作(如 getDate(), getFullYear() 等) 可以使用 Date() 对象执行

for example: I want the string of utc format like "2019-10-31T00:00:00.000Z" to get converted to utc date and perform operations(like getDate(), getFullYear(), etc) which could be performed using Date() object

推荐答案

来自 Date 对象上的 ="noreferrer">MDN 文档:

From the MDN Documentation on the Date object:

创建一个 JavaScript Date 实例,它以独立于平台的格式表示单个时刻.Date 对象包含一个 Number 表示自 UTC 1970 年 1 月 1 日以来的毫秒数.

Creates a JavaScript Date instance that represents a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.

换句话说,Date 对象只是一个包含单个值的抽象,并且该值总是以 UTC 表示.如果您想查看该值,请调用 .valueOf().getTime(),或将其强制为数字(例如:+d).

In other words, the Date object is simply an abstraction that contains a single value, and that value always in terms of UTC. If you want to see that value, call either .valueOf() or .getTime(), or coerce it to a Number (ex: +d).

Date 对象的许多功能可以以不同的方式公开该值,例如以标准化的字符串格式、人类可读的字符串格式或表示部分组件值的各种其他数字(即年、月、日、小时、分钟、秒、毫秒).其中一些函数在确定其输出时使用 UTC(例如 .toISOString().getUTCFullYear() 等),而其他函数在确定其输出时使用计算机的本地时区确定它们的输出(例如 .toString().getFullYear() 等).

There are many features of the Date object that can expose that value in different ways, such as in a standardized string format, a human readable string format, or various other numbers that represent partial component values (ie. year, month, day, hour, minute, second, millisecond). Some of these functions use UTC when determining their output (such as .toISOString(), .getUTCFullYear(), etc.), and others use the computer's local time zone when determining their output (such as .toString(), .getFullYear(), etc.).

很容易对此感到困惑,尤其是在 Date 对象上直接调用 console.log 时.需要记住的是,控制台日志记录功能是特定于实现的.您可能会看到一个人类可读的字符串,它看起来像本地时间,就像您调用了 .toString() 一样,但是某些实现会为您提供一个看起来像 ISO 格式的 UTC 时间的字符串,就像您调用了 .toISOString().确实,实现可以在日志记录方面做任何想做的事情.它可以打印出一只嘶嘶声的猫.

It's easy to get confused by this, especially if calling console.log directly on a Date object. The thing to remember there is that the console logging functionality is implementation specific. You may see a human readable string that looks like local time as if you called .toString(), but some implementations give you a string that looks like UTC time in ISO format as if you called .toISOString(). Really, the implementation can do whatever it wants when it comes to logging. It could print out a hissing cat.

更令人困惑的是,Date 构造函数根据本地时间获取其数字参数,因此人们可能会认为 new Date(2019,8,5,0,0,0) 是本地时间的 Date 对象,但实际上它在构造过程中转换为 UTC 并记录相应的时间戳.可以通过 new Date(Date.UTC(2019,8,5,0,0,0)) 将这些值解释为 UTC,但 Date 对象本身无论哪种方式都采用 UTC.

To add to the confusion, the Date constructor takes its numeric parameters in terms of local time, so one might think new Date(2019,8,5,0,0,0) was a Date object in local time, but actually it's converting to UTC during construction and recording the corresponding timestamp. One can pass new Date(Date.UTC(2019,8,5,0,0,0)) to have those values interpreted as UTC, but the Date object itself is in UTC either way.

关键是,不要认为因为您看到本地时间,Date对象就本地时间.不是.它从来都不是.因此,在 不同 时区请求它也是不可能的.在 UTC 中要求它是你已经拥有的.

The key point is, don't think that because you see local time that the Date object is in local time. It isn't. It never is. Thus, asking for it in a different time zone is also not possible. Asking for it in UTC is what you already have.

我希望像 "2019-10-31T00:00:00.000Z" 这样的 utc 格式字符串转换为 utc 日期并执行操作(如 getDate()getFullYear() 等)

I want the string of utc format like "2019-10-31T00:00:00.000Z" to get converted to utc date and perform operations(like getDate(), getFullYear(), etc)

您不会调用 getDategetFullYear.这些函数在内部询问计算机的本地时区并将其应用于时间戳,然后再确定其结果.相反,您可以调用 getUTCDategetUTCFullYear,它们不适用本地时区.

You wouldn't call getDate or getFullYear. Those functions internally ask for the computers's local time zone and apply it to the timestamp before determining their result. Instead you would call for getUTCDate or getUTCFullYear, which do not apply the local time zone.

...实际上我正在使用react-date-picker....(来自您的评论)

... Actually I am using react-date-picker. ... (from your comments)

有很多日期选择器.如果您没有从正在使用的功能中获得功能,请尝试另一个.

There are lots of date pickers out there. If you don't get the functionality out of the one you're using, try another.

当涉及到时区时,最好的人会做两件事之一:

The best ones will do one of two things when it comes to time zones:

  • 他们可能会为选择器的模式"提供设置,通常是本地或 UTC,或者可能是特定的 UTC 偏移量或时区 ID.这将允许您预先确定您希望选择器在其输入和输出方面的行为方式.

  • They might offer a setting for the "mode" of the picker, typically either local or UTC, or perhaps a specific UTC offset or time zone ID. This would allow you to pre-determine how you want the picker to behave with regard to its inputs and outputs.

他们可能只是将输入和输出作为 String 提供,而不是作为 Date 对象.这将允许选择器以中立的方式运行,然后您可以以您喜欢的任何方式解释用户的选择.(顺便说一句,HTML5 的 就是这样工作的.)

They might simply provide the input and output as a String, rather than as a Date object. This would allow the picker to behave in a neutral way, and you would then interpret the user's selection in whichever manner you pleased. (BTW, HTML5's <input type="date"> works this way.)

不幸的是,有很多日期选择器以这种方式工作,并假设您只想要一个基于本地时间创建的 Date 对象.为了解决这个问题,你需要撒一点谎.

Unfortunately, there are lots of date pickers that don't work that way and assume you simply want a Date object that is created based on local time. To contend with that, you will need to lie a little bit.

例如,假设我从假定我的本地时区的日期选择器中选择 2019-09-05.它在内部执行 new Date(2019, 8, 5),或者它可能执行 new Date("2019-09-05T00:00:00.000").在 Date 对象记录相应的时间戳值之前,任何一种形式都将被解释为本地时间.但我实际上不想那样 - 我希望用户选择一个 UTC 日期.所以我添加了这段代码来调整从选择器返回的对象(d):

For example, say I pick 2019-09-05 from a date picker that assumes my local time zone. It does internally new Date(2019, 8, 5), or perhaps it does new Date("2019-09-05T00:00:00.000"). Either form would be interpreted as local time before the the Date object records the corresponding timestamp value. But I didn't actually want that - I wanted the user to pick a UTC date. So I add this bit of code to adjust the object (d) returned from the picker:

d.setTime(d.getTime() - (d.getTimezoneOffset() * 60 * 1000));

这种技术被称为纪元转移,因为它使时间戳的基础远离(或朝向)1970-01-01T00:00:00.000Z Unix时代.在本例中,错误创建的是原始 Date 对象(由选择器创建的对象),因此上述将其移回标准 Unix 纪元.

This technique is called epoch shifting, as it shifts the basis of the timestamp away from (or towards) the 1970-01-01T00:00:00.000Z Unix epoch. In this case, it's the original Date object (the one created by the picker) that was created incorrectly, and thus the above shifts it back to the standard Unix epoch.

请记住,这项技术就是让您的用户选择一个基于 UTC 的值,并将其恢复为常规的 Date 对象.用于让您的用户选择一个本地值并将其转换为UTC.为此,您必须使用为此目的而设计的函数(getUTCFullYeargetISOString 等).

Keep in mind that this technique is all about having your user pick a UTC-based value, and get it back to a regular Date object. It is not to be used to have your user pick a local-value and convert it to UTC. For that you must used the functions designed for that purpose (getUTCFullYear, getISOString, etc.).

这篇关于在javascript中获取UTC日期(不是UTC字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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