JS: Date Tutorial
Quick usage code example
// create a date object of current date time const xdate = new Date(); const hh = xdate.getHours(); const mm = xdate.getMinutes(); const ss = xdate.getSeconds(); console.log(hh + ":" + mm + ":" + ss); // 8:15:51 console.log(xdate.getFullYear()); // 2020 console.log(xdate.getDate()); // 6 // first day of month is 1 console.log(xdate.getMonth()); // 8 // warning: Jan is 0, Feb is 1, etc console.log(xdate.getDay()); // 0 // Sunday is 0. Saturday is 6
Create Date Object (of current date)
console.log(new Date()); // 2026-02-04T05:54:31.574Z
Create Date Object (of specific date), parse date
ISO date string to milliseconds
// ISO date string to milliseconds console.log(Date.parse("2026-02-03T21:57:23-0800")); // 1770184643000
Milliseconds to date object
// ISO date string to milliseconds console.log(Date.parse("2026-02-03T21:57:23-0800")); // 1770184643000 console.log(new Date(1770184643000)); // 2026-02-04T05:57:23.000Z
Date object to milliseconds
const xdate = new Date(); console.log(xdate.getTime()); // 1770247765861
Print date in different formats
// print date in different formats console.log((new Date()).toLocaleString()); // 2/3/2026, 10:13:50 PM console.log((new Date()).toLocaleDateString()); // 2/3/2026 console.log((new Date()).toLocaleTimeString()); // 10:14:35 PM console.log((new Date()).toDateString()); // Tue Feb 03 2026 console.log((new Date()).toTimeString()); // 22:15:03 GMT-0800 (Pacific Standard Time) console.log((new Date()).toISOString()); // 2026-02-04T06:15:14.346Z console.log((new Date()).toUTCString()); // Wed, 04 Feb 2026 06:15:24 GMT console.log((new Date()).toString()); // Tue Feb 03 2026 22:16:12 GMT-0800 (Pacific Standard Time)
What is the difference between UTC and GMT?
For practical purposes, they are the same.
UTC (Coordinated Universal Time) is a time standard based on atomic clock.
GMT (Greenwich Mean Time) is a older time standard.
“Zulu time” is a term used in military and is the same as GMT. (GMT and UTC has a abbrev of Z, because Z is like zero. Z in NATO phonetic alphabet is read as Zulu. That's why GMT/UTC is also said Zulu time. )