mysql 时间戳 javascript_在同一日期在Mysql和Javascript中获得不同的时间戳?

bd96500e110b49cbb3cd949968f18be7.png

In Javascript

new Date()

Tue Mar 18 2014 18:54:17 GMT+0000 (GMT)

Date.UTC(2014,03,18)

1397779200000

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

解决方案

There are three differences:

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() 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.

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.

References are cited below.

JavaScript

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

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.

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

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

Also, as documented under 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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值