在同一日期在Mysql和Javascript中获得不同的时间戳? [英] Getting different timestamp in Mysql and Javascript for the same date?

查看:127
本文介绍了在同一日期在Mysql和Javascript中获得不同的时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用Java语言

    new Date()
    Tue Mar 18 2014 18:54:17 GMT+0000 (GMT)
    Date.UTC(2014,03,18)
    1397779200000

在Mysql中

mysql> SELECT  NOW(), UTC_TIMESTAMP();
+---------------------+---------------------+
| NOW()               | UTC_TIMESTAMP()     |
+---------------------+---------------------+
| 2014-03-18 18:55:04 | 2014-03-18 18:55:04 |
+---------------------+---------------------+

mysql> select UNIX_TIMESTAMP('2014-03-18') ;
+------------------------------+
| UNIX_TIMESTAMP('2014-03-18') |
+------------------------------+
|                   1395118800 |


推荐答案

存在三处区别:


  1. Date.UTC()用UTC解释其参数,而 UNIX_TIMESTAMP()在数据库会话的时区中解释其参数。从对问题的更新看来,这可能没有任何效果,因为数据库会话的本地时区可能是UTC。

  1. Date.UTC() interprets its arguments in UTC, whereas UNIX_TIMESTAMP() interprets its arguments in the database session's timezone. From the update to your question it appears that this may not have had any effect as the database session's local timezone might be in UTC.

Date.UTC()返回自UNIX时代以来的毫秒数,而 UNIX_TIMESTAMP()返回自UNIX时代以来的秒数:因此它们总是相差1000倍。

Date.UTC() returns a value in milliseconds since the UNIX epoch, whereas UNIX_TIMESTAMP() returns a value in seconds since the UNIX epoch: so they will always differ by a factor of 1000.

Date.UTC()是零索引的,因此 03 的值表示四月,而赋予 UNIX_TIMESTAMP()的日期文字表示三月

The month argument to Date.UTC() is zero-indexed, so a value of 03 indicates April whereas the date literal given to UNIX_TIMESTAMP() indicates March.

参考文献如下。

Date.UTC() (添加了强调):

As documented under Date.UTC() (emphasis added):


UTC 函数与 Date 构造函数的不同之处在于两个方面:它返回时间值作为数字,而不是创建Date对象,并且它以UTC而不是当地时间来解释参数。

The UTC function differs from the Date constructor in two ways: it returns a time value as a Number, rather than creating a Date object, and it interprets the arguments in UTC rather than as local time.

此外,如 TimeClip() (添加了强调):

Also, as documented under TimeClip() (emphasis added):


运算符TimeClip从其参数计算出毫秒数,该参数必须是ECMAScript数字值。

The operator TimeClip calculates a number of milliseconds from its argument, which must be an ECMAScript Number value.

此外,如月数


月由0到11之间的整数标识。

Months are identified by an integer in the range 0 to 11, inclusive.

[ deletia ]

月份值0指定一月; 1表示2月; 2表示3月; 3表示四月; 4表示五月; 5表示6月; 6表示7月; 7表示8月; 8表示9月; 9表示十月; 10指定11月;和11指定12月。

A month value of 0 specifies January; 1 specifies February; 2 specifies March; 3 specifies April; 4 specifies May; 5 specifies June; 6 specifies July; 7 specifies August; 8 specifies September; 9 specifies October; 10 specifies November; and 11 specifies December.



MySQL



UNIX_TIMESTAMP( 日期 (强调):

MySQL

As documented under UNIX_TIMESTAMP(date) (emphasis added):


如果< a href = http://dev.mysql.com/doc/zh-CN/date-and-time-functions.html#function_unix-timestamp rel = nofollow> UNIX_TIMESTAMP() ,它从 1970-01-01 00:00:00 UTC开始以 seconds 的形式返回该参数的值。服务器将 date 解释为当前时区中的值 并将其转换为UTC的内部值。客户端可以按照第10.6节 MySQL服务器时区中的说明设置时区支持。

If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC. The server interprets date as a value in the current time zone and converts it to an internal value in UTC. Clients can set their time zone as described in Section 10.6, "MySQL Server Time Zone Support".

这篇关于在同一日期在Mysql和Javascript中获得不同的时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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