ā¤ Like
šŸ”– Save
šŸ”— Share
Eric Hu
Eric Hu

JavaScript Date Functions

@thedevspaceio

šŸ“… The Most Hated Part of JavaScript: Date

JavaScript's Date object has a reputation for being terrible, and honestly, much of it is deserved. But with the right understanding, it becomes manageable.

In this cheatsheet, we'll learn how to create, read, modify, and format dates with the built-in Date object.

āœ… Creating dates āœ… Getting date parts āœ… Setting date parts āœ… Formatting dates āœ… Comparing dates āœ… Timestamps āœ… Useful patterns

#javascript #date #datetime #formatting #webdev #frontend #coding #tips #time


Creating dates

The Date object in JavaScript represents a single moment in time, stored as the number of milliseconds since January 1, 1970 UTC.

js
const now = new Date(); // current date and time
const specific = new Date("2024-07-12"); // from ISO string
const fromParts = new Date(2024, 6, 12, 10, 30, 0); // year, month (0-based), day
const fromTimestamp = new Date(1720771200000); // from milliseconds

Getting date parts

MethodDescriptionExample
getFullYear()Four-digit year2024
getMonth()Month index (0-11)6 for July
getDate()Day of the month (1-31)12
getDay()Day of the week (0-6)5 for Friday
getHours()Hour (0-23)10
getMinutes()Minutes (0-59)30
getSeconds()Seconds (0-59)0
getMilliseconds()Milliseconds (0-999)0
getTime()Timestamp in milliseconds1720771200000

js
const date = new Date("2024-07-12T10:30:00");
 
date.getFullYear(); // 2024
date.getMonth(); // 6
date.getDate(); // 12
date.getHours(); // 10

Setting date parts

js
const date = new Date();
 
date.setFullYear(2025);
date.setMonth(0); // January
date.setDate(1);
date.setHours(9, 0, 0, 0);

Formatting dates

Use toLocaleDateString and toLocaleTimeString for localized formatting.

js
const date = new Date("2024-07-12T10:30:00");
 
date.toLocaleDateString("en-US"); // "7/12/2024"
date.toLocaleDateString("en-GB"); // "12/07/2024"
date.toLocaleTimeString("en-US"); // "10:30:00 AM"

Formatting with options

Pass an options object to control the output format.

js
const date = new Date("2024-07-12T10:30:00");
 
const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
};
 
date.toLocaleDateString("en-US", options); // "Friday, July 12, 2024"

Time formatting options.

js
const timeOptions = {
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
  hour12: false,
};
 
date.toLocaleTimeString("en-US", timeOptions); // "10:30:00"

ISO string

Convert a date to an ISO 8601 string.

js
const date = new Date("2024-07-12T10:30:00Z");
date.toISOString(); // "2024-07-12T10:30:00.000Z"

UTC methods

Use UTC methods when working with universal time.

js
const date = new Date("2024-07-12T10:30:00Z");
 
date.getUTCFullYear(); // 2024
date.getUTCMonth(); // 6
date.getUTCDate(); // 12
date.getUTCHours(); // 10

Comparing dates

Convert dates to timestamps for comparisons.

js
const a = new Date("2024-07-12");
const b = new Date("2024-07-15");
 
a < b; // true āŒ
a.getTime() === b.getTime(); // false āœ…

Timestamps

Get the current timestamp in milliseconds.

js
Date.now(); // 1720771200000
 
const start = Date.now();
// ... do work
const end = Date.now();
console.log(end - start); // elapsed milliseconds

Full-Stack AI Developer Roadmap

From HTML & CSS to working with AI models, all in one structured roadmap.

@thedevspaceio
www.thedevspace.io