Get the Date String in Milliseconds

This is a cross-browser way to get a UNIX date string using Javascript.

Date.now() returns the date in modern browsers, however older browsers do not recognize it.  So if(!Date.now) is then create a function for the older browser to use. Otherwise the function isn't created.

if (!Date.now) { // for compatibility with browsers that don't suppport Date.now()
    Date.now = function now() { return new Date().getTime(); }
}

time = Date.now();

Example:

Javascript

var timeStart - Date.now();
    // do some long calculations ...
var timeEnd = Date.now();
document.getElementById("runtime").innerHTML = (timeEnd - timeStart);

Produces the result: 

The element with id = "runtime" is assigned the value of the length of time the script ran.

Creates a numeric string such as 1521918603730

 

Copyright © Lage.us Website Development | Disclaimer | Privacy Policy | Terms of Use