Skip to content
Advertisements

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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: