Home >Web Front-end >JS Tutorial >How Can I Get a Timestamp in JavaScript?

How Can I Get a Timestamp in JavaScript?

Barbara Streisand
Barbara StreisandOriginal
2024-12-21 14:10:12749browse

How Can I Get a Timestamp in JavaScript?

Getting 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.

Unix Timestamp (Seconds)

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

Millisecond Timestamp (Precise)

For a timestamp with millisecond precision, employ:

Date.now()

Equivalent methods include:

+new Date()
new Date().valueOf()

Millisecond Timestamp (High Resolution)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn