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

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

问题描述

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

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

当我注意到在 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天全站免登陆