How to Obtain Timestamp in JavaScript
To obtain a representation of the current date and time as a single numerical value, similar to a Unix timestamp, JavaScript provides several methods.
Timestamp in Milliseconds:
To retrieve the number of milliseconds since the Unix epoch, use Date.now():
Date.now();
Alternatively, for compatibility with older browsers, you can use the following:
+new Date();
Timestamp in Seconds (Unix Timestamp):
To obtain the number of seconds since the Unix epoch, commonly known as the Unix timestamp, calculate the quotient of Date.now() divided by 1000:
Math.floor(Date.now() / 1000);
Higher Resolution Timestamp in Milliseconds:
For a higher resolution timestamp in milliseconds, use performance.now():
var isPerformanceSupported = ( window.performance && window.performance.now && window.performance.timing && window.performance.timing.navigationStart ); var timeStampInMs = ( isPerformanceSupported ? window.performance.now() + window.performance.timing.navigationStart : Date.now() );
The above is the detailed content of How Do I Get a Unix Timestamp (or Milliseconds Since Epoch) in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!