Commonly Used JavaScript Date Objects
2. The Date() Object
The Date() object allows you to create dates using JavaScript. To create a new date, refer to the code below:
var d = new Date();
When you print the value of d, the date is printed in the following format:
Mon Jan 04 2021 11:21:19 GMT+0800 (China Standard Time)
This is a string, and the time zone is based on your browser’s time zone. The date and time displayed is the time at the moment that your code was run.
Dates are stored in JavaScript as milliseconds. The starting time is January 1, 1970, 00:00:00 UTC (Universal Standard Time).
When there are no arguments passed to the Date()object, the date given is the date and time at the given moment that the code was run. If a specific date needs to be set, the Date() object accepts multiple arguments for setting that specific date. You can set a specific data using these arguments. The format is as follows:
var d = new Date(year, month, day, hour, minute, second);
Example:
var d = new Date(2021, 0, 04, 10, 3, 59);
Output: Mon Jan 04 2021 10:03:59 GMT+0800 (China Standard Time)

The minute, second, hour, and day arguments can be eliminated when specifying dates. When specifying dates, you need at least two arguments, the year and the month. When only a single number x is provided as an argument, that number is considered as milliseconds and the date created will be the date at x milliseconds from time zero. Recall that time zero for JavaScript is January 1, 1970, 00:00:00 UTC.