Date.UTC() method. This method returns the number of milliseconds since January 1, 1970, 00:00:00, universal time.
Usage of Date.UTC()
The syntax for using
Date.UTC() is as follows:
Date.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]])
This method accepts between two and seven arguments:
- year: A four-digit number representing the year.
- month: An integer between 0 (January) and 11 (December).
- day (optional): An integer from 1 to 31 representing the day of the month. If omitted, it defaults to 1.
- hour (optional): An integer from 0 to 23 representing the hour. If omitted, it defaults to 0.
- minute (optional):An integer from 0 to 59 representing minutes. If omitted, it defaults to 0.
- second (optional):An integer from 0 to 59 representing seconds. If omitted, it defaults to 0.
- millisecond (optional):An integer from 0 to999 representing milliseconds. If omitted, it defaults to 0.
A Practical Example of Date.UTC()
// Create a date with Date.UTC var utcDate = new Date(Date.UTC(2022, 11, 25, 3, 0, 0)); console.log(utcDate); // expected output : Fri Dec 25 2022 03:00:00 GMT+0000 (Coordinated Universal Time)
In this example, we’re creating a date which represents December 25th, 2022 at exactly three o’clock in the morning (UTC).
Remember that while the month argument starts from zero (January), the day of the month starts from one. Also note that all arguments after year and month are optional and will default to their lowest values if not provided.
We hope this tutorial has helped you understand how to use the
Date.UTC() method effectively!