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

前端 未结 4 875
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  • 2021-01-04 11:55

    The first version converts a Date to a string and parses it, which is a pretty pointless thing to do - and in some cases could lose information, I suspect. (Imagine during a DST transition, when the clock goes back - the same local times occur twice for that hour, and I don't know offhand whether the string representation would differentiate between the two occurrences.)

    The second is significantly cleaner in my view. In general, you should avoid string conversions when you don't need them - they can often lead to problems, and there's nothing in what you're trying to do which is inherently about a string representation.

    Unless you actually need the Date elsewhere, it would be simpler to use:

    ms = new Date().getTime()
    

    Or even better, use the static now() method:

    ms = Date.now()
    
    0 讨论(0)
  • 2021-01-04 12:01

    Date.parse(dt)

    The parse function applies the ToString operator to its argument and interprets the resulting string as a date; it returns a number, the UTC time value corresponding to the date. The string may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the string.

    getTime()

    Returns the numeric value corresponding to the time for the specified date according to universal time.

    0 讨论(0)
  • 2021-01-04 12:07

    Performance would be the big difference. In the both cases, you allocate a Date instance. In the first example, you pass the date instance into parse() which expects a String. The JavaScript engine will call toString() on the Date which will also allocate a String for the date. Basically, it is the same as:

    dt = new Date();             // allocate a Date
    dateString = dt.toString();  // allocate a String
    ms = Date.parse(dateString); // Total: 2 allocations
    

    In the second example, you are calling the getTime() method on the Date instance which will eliminate the String allocation.

    dt = new Date();             // allocate a Date
    ms = dt.getTime();           // Total: 1 allocation
    

    Another option to eliminate all allocations would be to call Date.now():

    ms = Date.now();             // Total: 0 allocations
    

    This directly returns the time in ms without constructing the other objects.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题