在编写web应用程序时,将(服务器端)所有日期时间作为UTC时间戳存储在DB中是有意义的。

当我注意到在JavaScript中无法在时区操作方面原生做很多事情时,我感到很惊讶。

我稍微扩展了Date对象。这个函数有意义吗?基本上,每次我向服务器发送任何东西,它都将是一个用这个函数格式化的时间戳…

这里有什么主要问题吗?或者换个角度解决?

Date.prototype.getUTCTime = function(){ 
  return new Date(
    this.getUTCFullYear(),
    this.getUTCMonth(),
    this.getUTCDate(),
    this.getUTCHours(),
    this.getUTCMinutes(), 
    this.getUTCSeconds()
  ).getTime(); 
}

我只是觉得有点费解。我对表现也不是很确定。


当前回答

如果你想要一行,UTC Unix时间戳可以在JavaScript中创建:

var currentUnixTimestap = ~~(+new Date() / 1000);

这将考虑到系统的时区。它基本上是以秒为单位的从epoch开始的时间。

工作原理:

创建日期对象:new date()。 通过在对象创建之前添加一元+将其转换为时间戳整数来转换为时间戳。: +new Date()。 将毫秒转换为秒:+new Date() / 1000 使用双波浪号将值舍入为整数。: ~~(+new Date())

其他回答

因为new Date().toUTCString()返回一个像“Wed, 11 Oct 2017 09:24:41 GMT”这样的字符串,你可以将最后3个字符切片并将切片后的字符串传递给new Date():

new Date()
// Wed Oct 11 2017 11:34:33 GMT+0200 (CEST)

new Date(new Date().toUTCString().slice(0, -3))
// Wed Oct 11 2017 09:34:33 GMT+0200 (CEST)

一旦你这样做了

new Date(dateString).getTime() / 1000

它已经是UTC时间戳了

   const getUnixTimeUtc = (dateString = new Date()) => Math.round(new Date(dateString).getTime() / 1000)

我在https://www.unixtimestamp.com/index.php上进行了测试

I actually think Date values in js are far better than say the C# DateTime objects. The C# DateTime objects have a Kind property, but no strict underlying time zone as such, and time zone conversions are difficult to track if you are converting between two non UTC and non local times. In js, all Date values have an underlying UTC value which is passed around and known regardless of the offest or time zone conversions that you do. My biggest complaint about the Date object is the amount of undefined behaviour that browser implementers have chosen to include, which can confuse people who attack dates in js with trial and error than reading the spec. Using something like iso8601.js solves this varying behaviour by defining a single implementation of the Date object.

默认情况下,规范说您可以使用扩展的ISO 8601日期格式创建日期,如

var someDate = new Date('2010-12-12T12:00Z');

因此,您可以通过这种方式推断出准确的UTC时间。

当您希望将Date值传递回服务器时,您将调用

someDate.toISOString();

或者如果您更愿意使用毫秒时间戳(从UTC 1970年1月1日开始的毫秒数)

someDate.getTime();

ISO 8601 is a standard. You can't be confused about what a date string means if you include the date offset. What this means for you as a developer is that you never have to deal with local time conversions yourself. The local time values exist purely for the benefit of the user, and date values by default display in their local time. All the local time manipulations allow you to display something sensible to the user and to convert strings from user input. It's good practice to convert to UTC as soon as you can, and the js Date object makes this fairly trivial.

缺点是,没有太多的范围可以强制客户端的时区或地区(据我所知),这对于特定于网站的设置来说是很烦人的,但我猜这背后的原因是,这是一个不应该被触及的用户配置。

因此,简而言之,没有很多本地时区操作支持的原因只是因为您不想这样做。

你也可以使用getTimezoneOffset和getTime,

x =新日期() var utccos = (x.getTime() + x. gettimezone胶印()*60*1000)/1000; 游戏机。log (UTCseconds”、“UTCseconds)

这将返回UTC时间戳:

var utc = new Date(new Date().toUTCString()).getTime();