Timestamp in JavaScript

Little tutorial with code examples and video on timestamp in JavaScript
11 September 2017   2341

In order to get a timestamp in JavaScript, use this code. 

var timeStampInMs = window.performance && window.performance.now && window.performance.timing && window.performance.timing.navigationStart ? window.performance.now() + window.performance.timing.navigationStart : Date.now();

console.log(timeStampInMs, Date.now());

In short:

+ new Date()

A unary operator like plus triggers the valueOf method in the Date object and it returns the timestamp (without any alteration).

Details:

On almost all current browsers you can use Date.now() to get the UTC timestamp in milliseconds; a notable exception to this is IE8 and earlier.

You can easily make a shim for this, though:

if (!Date.now) {
    Date.now = function() { return new Date().getTime(); }
}

To get the timestamp in seconds, you can use:

Math.floor(Date.now() / 1000)

Or alternatively you could use:

Date.now() / 1000 | 0

Which should be slightly faster, but also less readable.

Note that using Date.now() is slightly better because it's shorter & doesn't create a new Date object. However, if you don't want a shim & maximum compatibility, you could use the "old" method to get the timestamp in milliseconds:

new Date().getTime()

Which you can then convert to seconds like this:

Math.round(new Date().getTime()/1000)

And you can also use the valueOf method which we showed above:

new Date().valueOf()

Chrome 73 Released

Now the Android version of popular browser has Lite page mode that speeds up website download and reduces traffic consumption
13 March 2019   228

Google has released another update Chrome.

  • The Android application has Lite pages mode for optimizing pages, speeding up their loading and reducing traffic, as well as a new download manager.
  • Settings for account management and synchronization are collected in a separate section - Sync and Google services.
  • Improved video mode “picture in picture”. A floating window can now make web applications (PWA): chat rooms, instant messengers, video conferencing. And added the button "Skip advertising".
  • It is forbidden to load external resources in scripts provided by extensions. Extension developers now need to use background handlers.

The previous version was released in late January.