Difference between Date.parse() and .getTime()

前端 未结 4 900
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 11:33

What\'s the main difference between:

dt = new Date();
ms = Date.parse(dt);

and

dt = new Date();
ms = dt.getTime();
<         


        
4条回答
  •  梦毁少年i
    2021-01-04 12:12

    Though it's an old post, I'll leave my answer for someone who visit here later than me.

    dt = new Date();
    
    // often false, occasionally true
    Date.parse(dt) === dt.getTime()
    

    This is because you will lose the information about milliseconds when you do dt.toString() which is internally called by Date.parse(dt). At least for Chrome (63.0.3239.84) and Firfox Quantum (57.0.3), they implement the toString() method of Date object like this way. You can try the following example yourself.

    dt = new Date('2018.1.1 01:01:01.001')
    dt.getTime() // 1514739661001
    Date.parse(dt) // 1514739661000
    

    Date.parse(dt) equals to dt.getTime() only if dt is captured at the moment that millisecond equals 0.

提交回复
热议问题