Home >Web Front-end >JS Tutorial >How Can I Get a Timestamp in JavaScript?
Timestamping enables the generation of a unique numerical representation of a particular point in time. In JavaScript, several methods can be employed to obtain a timestamp.
To acquire a timestamp in the Unix format, representing the number of seconds since the Unix epoch (January 1, 1970), utilize:
Math.floor(Date.now() / 1000)
Alternatively, for higher-speed execution, you can use:
Date.now() / 1000 | 0
For a timestamp with millisecond precision, employ:
Date.now()
Equivalent methods include:
+new Date()
new Date().valueOf()
To access a millisecond timestamp with improved resolution, utilize:
var timeStampInMs = ( isPerformanceSupported ? window.performance.now() + window.performance.timing.navigationStart : Date.now() );
where isPerformanceSupported is a boolean indicating support for performance profiling.
The above is the detailed content of How Can I Get a Timestamp in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!