JavaScript Date.now()

Why Date.now ?

If we only want to measure the difference, we don’t need the Date object. There’s a special method Date.now() that returns the current timestamp.

It is semantically equivalent to new Date().getTime(), but it doesn’t create an intermediate Date object. So it’s faster and doesn’t put pressure on garbage collection. It is used mostly for convenience or when performance matters, like in games in JavaScript or other specialized applications.

Syntax:

Date.now()


JavaScript Date.now Examples

Example 1

        let minutes = 1000 * 60;
        let hours = minutes * 60;
        let days = hours * 24;
        let years = days * 365;
        let date = Date.now();

        let print = Math.round(date / years);
        console.log(print)

output:

49


Example 2

        // 1970 - 2019 
        let millisecond = Date.now();
        console.log(millisecond)

output:

1549391459096


Example 3

        var date = Date();

        a = date.toString()

        console.log("Date: " + date)

output:

Date: Tue Feb 05 2019 21:32:50 GMT+0300 (Arabia Standard Time)


Sources:


Browser Support

Chrome5
Edgeyes
Firefox3
Internet Explorer9
Opera10.5
Safari4
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started