napoleonu
V2EX  ›  问与答

Javascript求助,Unix时间戳和日期的转换问题

  •  
  •   napoleonu · Mar 6, 2011 · 5253 views
    This topic created in 5625 days ago, the information mentioned may be changed or developed.
    以下是我在Chrome console中的计算过程,结果让我迷惑

    没学过Javascript,只是看着别人这么写所以这样用。

    Math.round(new Date().getTime()/1000)
    1299408914

    Math.round(new Date(2011, 02, 01, 00, 00, 0).getTime()/1000)
    1298908800

    new Date(1298908800 * 1000).toLocaleString()
    "Tue Mar 01 2011 00:00:00 GMT+0800 (China Standard Time)"

    new Date(1299408914 * 1000).toLocaleString()
    "Sun Mar 06 2011 18:55:14 GMT+0800 (China Standard Time)"

    Math.round(new Date(2011, 03, 01, 00, 00, 0).getTime()/1000)
    1301587200

    new Date(1301587200 * 1000).toLocaleString()
    "Fri Apr 01 2011 00:00:00 GMT+0800 (China Standard Time)"
    5 replies    1970-01-01 08:00:00 +08:00
    napoleonu
        1
    napoleonu  
    OP
       Mar 6, 2011
    不知道为什么差一个月,,,
    darasion
        2
    darasion  
       Mar 6, 2011
    时间戳我经常这样用: Number(new Date)

    没看太懂。
    现在不是3月吗?你用一个2月的跟它比较,当然差一个月了。。
    darasion
        3
    darasion  
       Mar 6, 2011
    啊。查了一下手册。

    月份范围是 0-11 。
    napoleonu
        4
    napoleonu  
    OP
       Mar 6, 2011
    @darasion 谢谢,我也猜到了 :)
    shanghaikid
        5
    shanghaikid  
       Mar 6, 2011
    js是从0开始算的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3213 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 10:41 · PVG 18:41 · LAX 03:41 · JFK 06:41
    ♥ Do have faith in what you're doing.