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:
- https://www.geeksforgeeks.org
- https://developer.mozilla.org
- https://www.w3schools.com
- https://javascript.info
Browser Support
| Chrome | 5 |
| Edge | yes |
| Firefox | 3 |
| Internet Explorer | 9 |
| Opera | 10.5 |
| Safari | 4 |
| Android webview | yes |
| Chrome for Android | yes |
| Edge mobile | yes |
| Firefox for Android | 4 |
| Opera Android | yes |
Leave a comment