JavaScript new Date(milliseconds) function
The new Date(milliseconds)
function in JavaScript creates a new Date
object that represents the date and time corresponding to the number of milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC).
Syntax:
Parameters:
- milliseconds: A numeric value representing the number of milliseconds since the Unix Epoch (January 1, 1970).
Example:
1. Creating a Date from Milliseconds
Output:
Explanation:
- The
0
milliseconds represent the exact Unix Epoch (January 1, 1970, 00:00:00 UTC).
2. Using a Positive Number of Milliseconds
Output:
Explanation:
86400000
milliseconds represent 1 day after the Unix Epoch, which corresponds to January 2, 1970.
3. Using a Negative Number of Milliseconds
Output:
Explanation:
-86400000
milliseconds represent 1 day before the Unix Epoch, resulting in December 31, 1969.
4. Using a Large Number of Milliseconds
Output:
Explanation:
1700000000000
milliseconds represent November 4, 2023, at 22:46:40 UTC.
Summary:
new Date(milliseconds)
creates aDate
object based on the number of milliseconds since January 1, 1970.- Positive values represent dates after the Unix Epoch, while negative values represent dates before.
- You can use this method to calculate dates in the past or future relative to the Epoch by adding or subtracting milliseconds.