在 JavaScript 中
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 |
最佳答案
存在三个差异:
Date.UTC()
以 UTC 格式解释其参数,而UNIX_TIMESTAMP()
在数据库 session 的时区中解释其参数。从您问题的更新来看,这可能不会产生任何影响,因为数据库 session 的本地时区可能采用 UTC。Date.UTC()
返回自 UNIX 纪元以来以毫秒为单位的值,而UNIX_TIMESTAMP()
返回自 UNIX 纪元以来以秒为单位的值:因此它们始终相差 1000 倍。Date.UTC()
的月份参数是零索引的,因此值为03
表示四月,而给UNIX_TIMESTAMP()
的日期文字表示三月。
下面引用了引用文献。
JavaScript
如 Date.UTC()
下所述(强调):
The
UTC
function differs from theDate
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()
下所述(强调):
The operator TimeClip calculates a number of milliseconds from its argument, which must be an ECMAScript Number value.
此外,如 Month Number 下所述:
Months are identified by an integer in the range 0 to 11, inclusive.
[ deletia ]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(<strong><em>date</em></strong>)
下所述(强调):
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 interpretsdate
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”.
关于javascript - 在 Mysql 和 Javascript 中获取同一日期的不同时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22488144/