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');
- 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();
date.getMonth();
date.getDate();
date.getDay();
date.getHours();
date.getMinutes();
date.getSeconds();
- 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);
- 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());
- 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);
- 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'));
getTimezoneOffset()
returns offset in minutes
- Negative for UTC+, positive for UTC-
const offsetMinutes = new Date().getTimezoneOffset();
console.log(offsetMinutes);
toISOString()
gives UTC ISO 8601 format
- Great for APIs & storage
const iso = new Date().toISOString();
console.log(iso);
- Create a date from an ISO string
const date = new Date("2025-04-05T19:34:00.000Z");
console.log(date);
- 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'));
π οΈ 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 |