JavaScript Date.UTC() function
The Date.UTC()
function in JavaScript returns the number of milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC) for a specified UTC (Coordinated Universal Time) date and time.
Syntax:
Parameters:
- year: The full year (e.g., 2024).
- month: The month (0 for January, 11 for December). Note: Months are zero-indexed.
- day (optional): The day of the month (1-31). Default is 1.
- hours (optional): The hour of the day (0-23). Default is 0.
- minutes (optional): The minutes (0-59). Default is 0.
- seconds (optional): The seconds (0-59). Default is 0.
- milliseconds (optional): The milliseconds (0-999). Default is 0.
Returns:
- The number of milliseconds between the Unix Epoch and the given UTC date.
Example 1: Basic Date (Year, Month)
Output:
Explanation:
Date.UTC(2024, 9, 22)
represents October 22, 2024, at 00:00:00 UTC.- The function returns the timestamp in milliseconds:
1729555200000
.
Example 2: Full Date and Time
Output:
Explanation:
Date.UTC(2024, 9, 22, 14, 30, 15)
represents October 22, 2024, at 14:30:15 UTC.- The result is
1729607415000
milliseconds.
Example 3: Handling Zero-Indexed Month
Output:
Explanation:
- The month is zero-indexed, so
0
corresponds to January. Date.UTC(2024, 0, 1)
represents January 1, 2024, and returns1704067200000
milliseconds.
Summary:
Date.UTC()
generates a timestamp for a UTC date, allowing you to specify year, month, day, hours, minutes, seconds, and milliseconds.- Months are zero-indexed: January is 0, December is 11.
- It returns the number of milliseconds from January 1, 1970 (Unix Epoch) to the given UTC date.