JavaScript Date & Time Cheat Sheet

🟒 Create a New Date

  • new Date() gives current date & time
  • Pass a date string to create a specific date
const now = new Date();
const specific = new Date('2025-04-05T12:00:00');

πŸ“… Get Date Parts

  • Extract individual parts: year, month, day, etc.
  • Remember: getMonth() is 0-based (0=Jan)
  • getDay() returns weekday (0=Sun)
const date = new Date();
date.getFullYear(); // 2025
date.getMonth();    // 0-11
date.getDate();     // Day of month
date.getDay();      // 0-6 (Sun-Sat)
date.getHours();    // 0-23
date.getMinutes();  // 0-59
date.getSeconds();  // 0-59

πŸ“ Format Date Manually

  • Concatenate parts yourself
  • Adjust month by adding 1 (since it’s 0-based)
const date = new Date();
const formatted = `${date.getFullYear()}-${
  date.getMonth() + 1
}-${date.getDate()}`;
console.log(formatted); // e.g., "2025-4-5"

🌐 Format with Intl.DateTimeFormat

  • Use Intl.DateTimeFormat for localized formatting
  • Supports options like dateStyle & timeStyle
const formatter = new Intl.DateTimeFormat('en-US', {
  dateStyle: 'full',
  timeStyle: 'short',
});
formatter.format(new Date());
// Example: "Saturday, April 5, 2025 at 12:30 PM"

βž•βž– Add or Subtract Days

  • Safely add/subtract days by modifying a copy
const addDays = (date, days) => {
  const copy = new Date(date);
  copy.setDate(copy.getDate() + days);
  return copy;
};

const today = new Date();
const tomorrow = addDays(today, 1);
console.log(tomorrow);

πŸ”Ž Compare Dates

  • Check if one date is before another
  • Compare by toDateString() to ignore time
const isBefore = (a, b) => new Date(a) < new Date(b);

const isSameDay = (a, b) =>
  new Date(a).toDateString() === new Date(b).toDateString();

console.log(isBefore('2025-04-01', '2025-05-01')); // true

🌍 Get Timezone Offset

  • getTimezoneOffset() returns offset in minutes
  • Negative for UTC+, positive for UTC-
const offsetMinutes = new Date().getTimezoneOffset();
console.log(offsetMinutes); // e.g., -420 for PDT

πŸ”— Convert to ISO String

  • toISOString() gives UTC ISO 8601 format
  • Great for APIs & storage
const iso = new Date().toISOString();
console.log(iso); // e.g., "2025-04-05T19:34:00.000Z"

πŸ“₯ Parse ISO String

  • Create a date from an ISO string
const date = new Date("2025-04-05T19:34:00.000Z");
console.log(date);

⏱ Time Since (Relative Time)

  • Show human-friendly time differences
  • Calculate seconds, minutes, hours, days ago
const timeSince = date => {
  const seconds = Math.floor((new Date() - new Date(date)) / 1000);

  if (seconds < 60) return `${seconds} sec ago`;

  const minutes = Math.floor(seconds / 60);
  if (minutes < 60) return `${minutes} min ago`;

  const hours = Math.floor(minutes / 60);
  if (hours < 24) return `${hours} hrs ago`;

  const days = Math.floor(hours / 24);
  return `${days} days ago`;
};

console.log(timeSince('2025-04-05T12:00:00'));

πŸ“„ Quick Reference

πŸ› οΈ Method / Concept πŸ“‹ Description βœ… Example
new Date() Create current date/time const now = new Date()
new Date(string) Create from date string new Date("2025-04-05T12:00:00")
getFullYear() Get 4-digit year date.getFullYear() β†’ 2025
getMonth() Get month (0–11) date.getMonth() β†’ 3 (April)
getDate() Day of the month date.getDate() β†’ 5
getDay() Weekday (0–6, Sunday = 0) date.getDay() β†’ 6 (Saturday)
getHours() Hours (0–23) date.getHours() β†’ 14
getMinutes() Minutes (0–59) date.getMinutes() β†’ 45
toISOString() Convert to ISO string (UTC) date.toISOString() β†’ "2025-04-05T19:34:00.000Z"
getTimezoneOffset() Minutes behind/ahead of UTC date.getTimezoneOffset() β†’ -420 (PDT)
Intl.DateTimeFormat Localized format formatter.format(date)
setDate() Set day of the month date.setDate(date.getDate() + 7)
toDateString() Just the date (no time) date.toDateString() β†’ "Sat Apr 05 2025"
new Date().getTime() Timestamp in milliseconds Date.now() β†’ 1722989760000
new Date("...") < new Date("...") Compare two dates isBefore(a, b)
timeSince(date) Human-friendly "ago" time 5 min ago, 2 days ago