javascript - 在 Mysql 和 Javascript 中获取同一日期的不同时间戳?

标签 javascript mysql date timezone

在 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 |

最佳答案

存在三个差异:

  1. Date.UTC()以 UTC 格式解释其参数,而 UNIX_TIMESTAMP()在数据库 session 的时区中解释其参数。从您问题的更新来看,这可能不会产生任何影响,因为数据库 session 的本地时区可能采用 UTC。

  2. Date.UTC()返回自 UNIX 纪元以来以毫秒为单位的值,而 UNIX_TIMESTAMP()返回自 UNIX 纪元以来以秒为单位的值:因此它们始终相差 1000 倍。

  3. Date.UTC() 的月份参数是零索引的,因此值为 03表示四月,而给 UNIX_TIMESTAMP() 的日期文字表示三月。

下面引用了引用文献。

JavaScript

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() 下所述(强调):

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 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”.

关于javascript - 在 Mysql 和 Javascript 中获取同一日期的不同时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22488144/

相关文章:

mysql - 无法从本地 Windows 机器连接到远程 AWS Linux MySQL

php - 坚持使用 php 中的字符编码

datetime - 获取 PHP DatePeriod 对象的开始和结束日期?

java - 如何找出给定日期以及开始和结束时间的所有可用持续时间: java

javascript - 添加类 "if events"?

javascript - Intl.NumberFormat 货币 : $US currency symbol?

javascript - 将 HTML 表格导出到 Excel。用户必须能够选择文件的名称

javascript - Reactjs OnClick 添加更多列表

php - 具有 3 列的 MYsql GROUP_CONCAT 在 PHP 中执行

mysql - mysql 中的默认日期?