How do you get a timestamp in JavaScript?
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The following example demonstrates how to get a timestamp and how to convert it back to the human-readable date and time format.
How do you get a timestamp in JavaScript?
Timestamp in milliseconds
To get the number of milliseconds since Unix epoch, call
Date.now
:Alternatively, use the unary operator
+
to callDate.prototype.valueOf
:Alternatively, call
valueOf
directly:To support IE8 and earlier (see compatibility table), create a shim for
Date.now
:Alternatively, call
getTime
directly:Timestamp in seconds
To get the number of seconds since Unix epoch, i.e. Unix timestamp:
Alternatively, using bitwise-or to floor is slightly faster, but also less readable and may break in the future (see explanations 1, 2):