JavaScript: Date & Time

Date in ISO 8601 string format – UTC

let timeStr = "2022-05-14T13:14:15.001Z"; // Z indicates UTC

Date & Time processing based on the Date object.

let myDate = new Date(timeStr);
let myDate = new Date(epochMillis);
let myDate = new Date(); //now
let myDate = Date.now();
let myDate = new Date(year, month [Jan = zero], dayOfMonth, hour, minute, sec, millis);
let myDate = Date.UTC(year, month [Jan = zero], dayOfMonth, hour, minute, sec, millis);
let myDate = Date.parse(dateStr);

//Get “epoch millis” (milliseconds since Jan 1 1970)
myDate.getTime()

//to return to ISO string
myDate.toISOString()

Date instance methods

  • getYear (year after 1900)
  • getFullYear
  • getMonth (January = zero)
  • getDate (day of month)
  • getDay (day of week, Sunday = 0)
  • getHour
  • getMinute
  • getSeconds
  • getMilliseconds

Formatting

myDate.toLocaleString(“en-US”, {year:’numeric’, month: ‘long’, day: ‘numeric’})
myDate.toLocaleTimeString
myDate.toLocaleDateString