如何在JavaScript中获取UTC时间戳? [英] How do I get a UTC Timestamp in JavaScript?

查看:2067
本文介绍了如何在JavaScript中获取UTC时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While writing a web application, it makes sense to store (server side) all datetimes in the DB as UTC timestamps.

I当我注意到你在JavaScript中的时区操作方面做得不够时,我感到很惊讶。

I was astonished when I noticed that you couldn't natively do much in terms of Timezone manipulation in JavaScript.

我将Date对象扩展了一点。这个功能有意义吗?基本上,每次我向服务器发送任何内容时,它都将是使用此函数格式化的时间戳...

I extended the Date object a little. Does this function make sense? Basically, every time I send anything to the server, it's going to be a timestamp formatted with this function...

你能看到任何重大问题吗?或者从不同的角度来看解决方案?

Can you see any major problems here? Or maybe a solution from a different angle?

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

这对我来说似乎有点费解。我也不太了解表现。

It just seems a little convoluted to me. And I am not so sure about performance either.

推荐答案


  1. 日期构建方式使用当地时区,使构建日期不正确。设置某个日期对象的时区是从包含时区的日期字符串构造它。 (我在使用较旧的Android浏览器时遇到问题。)

  1. Dates constructed that way use the local timezone, making the constructed date incorrect. To set the timezone of a certain date object is to construct it from a date string that includes the timezone. (I had problems getting that to work in an older Android browser.)

请注意 getTime()返回毫秒,而不是普通秒。

Note that getTime() returns milliseconds, not plain seconds.

对于UTC / Unix时间戳,以下内容应该足够了:

For a UTC/Unix timestamp, the following should suffice:

Math.floor((new Date()).getTime() / 1000)

它会将当前时区偏移量计入结果。对于字符串表示, David Ellis 的答案有效。

It will factor the current timezone offset into the result. For a string representation, David Ellis' answer works.

澄清:

new Date(Y, M, D, h, m, s)

该输入被视为本地时间。如果传入 UTC时间,结果将有所不同。观察(我现在在GMT +02:00,现在是07:50):

That input is treated as local time. If UTC time is passed in, the results will differ. Observe (I'm in GMT +02:00 right now, and it's 07:50):

> var d1 = new Date();
> d1.toUTCString();
"Sun, 18 Mar 2012 05:50:34 GMT" // two hours less than my local time
> Math.floor(d1.getTime()/ 1000)
1332049834 

> var d2 = new Date( d1.getUTCFullYear(), d1.getUTCMonth(), d1.getUTCDate(), d1.getUTCHours(), d1.getUTCMinutes(), d1.getUTCSeconds() );
> d2.toUTCString();
"Sun, 18 Mar 2012 03:50:34 GMT" // four hours less than my local time, and two hours less than the original time - because my GMT+2 input was interpreted as GMT+0!
> Math.floor(d2.getTime()/ 1000)
1332042634

另请注意 getUTCDate()不能替换 getUTCDay()。这是因为 getUTCDate()返回 当天的某一天 ;然而, getUTCDay()返回 星期几

Also note that getUTCDate() cannot be substituted for getUTCDay(). This is because getUTCDate() returns the day of the month; whereas, getUTCDay() returns the day of the week.

这篇关于如何在JavaScript中获取UTC时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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