JS - Date Object

Objects that contain values that represent dates and times, these date objects can be formatted and changed.
const date1 = new Date()
console.log(date1)
// 2026-04-13T01:40:46.976Z
This gives me current date and time of my local machine.
Setting dates
// Date(year, month, day, hour, minute, second, ms)
const date2 = new Date(2024, 0, 1, 2, 3, 4, 5)
console.log(date2)
// Mon Jan 01 2024 02:03:04 GMT+0530
We can also set it using a string
const date2 = new Date("2024-01-02T12:00:00Z")
T -> Time
Z -> UTC
We can also set date using milliseconds.
So if we set it to zero we get the starting time or the "zero" for time.
const date2 = new Date(0)
console.log(date2)
// Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)
Now if we have a value in milliseconds it will store the date those milliseconds after the epic date.
const date2 = new Date(100000000000000)
console.log(date2)
// Wed Nov 16 5138 15:16:40 GMT+0530 (India Standard Time)
using set methods
const date = new Date()
date.setUTCFullYear(2026)
date.setUTCMonth(0)
date.setUTCDate(1)
date.setUTCHours(2)
date.setUTCMinutes(3)
date.setUTCSeconds(3)
console.log(date)
// 2026-01-01T02:03:03.052Z
console.log(date.toUTCString())
// Thu, 01 Jan 2026 02:03:03 GMT
omit the 'UTC' to set time in your local timezone, eg :- setFullYear()
Getting values
const date = new Date("2026-06-03T12:00:20Z")
console.log(date.getFullYear())
console.log(date.getMonth()) // starts from 0
console.log(date.getDate())
console.log(date.getHours())
console.log(date.getMinutes())
console.log(date.getSeconds())
console.log(date.getDay()) // starts from 0
Output
2026
5
3
17
30
20
3
Comparing dates
const date1 = new Date("2025-12-31")
const date2 = new Date("2026-01-01")
if(date2 > date1){
console.log("Happy 2026!")
}
// Output : Happy 2026!
These were all the basic methods one must know, for more methods refer MDN docs



