如何将时间设置为UTC [英] how to make the timezone of date to UTC

查看:318
本文介绍了如何将时间设置为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于将日期转换为UTC日期的过滤器。

I have a filter that i use to convert date to UTC date.

.filter('utc', function(){
    return function(val){
        var date = new Date(val);
        return new Date(date.getUTCFullYear(),
                date.getUTCMonth(),
                date.getUTCDate(),
                date.getUTCHours(),
                date.getUTCMinutes(),
                date.getUTCSeconds(),date.getTimezoneOffset());
    };
})

所以当我指定日期2016-06-18

So when i give the date 2016-06-18

它返回像这样的日期2016年6月18日星期六00:00:00 GMT-0400(东部夏令时间)

It returns the date like this Sat Jun 18 2016 00:00:00 GMT-0400 (Eastern Daylight Time)

日期是正确的,但它不能反映正确的时区。

The date is correct but its not reflecting the correct timezone.

我希望它以UTC时区返回,例如末尾的Z

I want it to return in UTC timezone, like Z at the end

推荐答案

几件事:


  • Date 构造函数没有接受时区关闭的参数等。 seconds 之后的参数是毫秒请参阅MDN参考

  • The Date constructor does not have a parameter that accepts a time zone offset. The parameter following seconds is milliseconds. Refer to the MDN reference.

您不应将基于UTC的值传递到 Date 构造函数中,而该构造函数应使用基于本地的值。这样做将具有跟踪完全不同的时间点的效果。您可能会认为自己正在调整时区偏移量,但实际上并非如此。

You should not pass UTC-based values into the Date constructor where local-based values are expected. Doing so will have the effect of tracking a completely different point in time. You may think you're adjusting for time zone offset, but you are not.

Date 对象总是具有以下行为:


  • 它跟踪单个值,由<$返回c $ c> .valueOf()(继承自 Object.valueOf )或 .getTime()。该值是自1970年1月1日午夜以来UTC以来经过的毫秒数(忽略igno秒)。

  • It tracks a single value, as returned by .valueOf() (inherited from Object.valueOf), or by .getTime(). That value is the number of milliseconds elapsed since Midnight at the start of Jan 1, 1970, in UTC (ignoring leap seconds).

绝大多数输入输出功能与 local 时区配合使用-即运行代码的计算机所设置的时区。与UTC一起使用的功能的名称通常带有 UTC

The vast majority of input and output functions work with the local time zone - that is, the time zone that is set by the computer where the code is running. The functions that work with UTC typically have "UTC" in their name.

它无法获得 Date 对象以使用其他时区。否则主张的任何解决方案都不会考虑边缘情况。特别是,时代转移是一种孤立使用的根本错误的通用方法。 (将UTC值传递给本地时间参数是一种时代转移的实现。)

It is not possible to get the Date object to use a different time zone. Any solution that claims otherwise is not considering edge cases. In particular, "epoch-shifting" is a common approach that is fundamentally wrong, when used in isolation. (Passing UTC values into local-time parameters is one implementation of epoch shifting.)

如果在寻找快速解决方案时,请考虑 .toUTCString() .toISOString()

If you're looking for a quick solution, consider .toUTCString() or .toISOString().


  • 某些浏览器可能还支持ECMAScript国际化API,并允许您执行以下操作: .toLocaleString(undefined,{timeZone:'UTC'})

  • Some browsers may also support the ECMAScript Internationalization API and let you do: .toLocaleString(undefined, { timeZone: 'UTC'})

Moment.js 是用于处理此类问题的非常常用的库,它填补了 Date 对象所存在的空白。本地到UTC的操作如下:

Moment.js is a very common library for dealing with these sorts of issue, and fills in a lot of gaps that the Date object has. Local-to-UTC is done like this:

moment(input, inputFormat).utc().format(outputFormat)




  • 请注意,瞬间通过时移实现了很多魔力,但是它通过公共API和新的对象类型(瞬间对象)将其隐藏,这提供了 Date 对象。

    • Note that moment does a lot of its magic via epoch-shifting, but it hides this via a public API and new object type (a moment object), which provides a layer of safety that you just can't get with the Date object alone.
    • 请注意,在您的问题中您根本没有说明什么在 val 变量中。根据是数字还是字符串以及字符串的格式, Date 对象的解析行为可能会发生很大变化。

      Note that in your question you did not at all state what was in the val variable. Depending on whether that is a number, or a string, and what format that string is in, the parsing behavior of the Date object may change considerably.

      这篇关于如何将时间设置为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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