JavaScript’s Date stores an instant (milliseconds since Unix epoch) and prints in the local timezone unless you use UTC methods or Intl. Parse ISO-8601 (2026-09-07T09:00:00+05:00) carefully: date-only "2026-09-07" is UTC midnight and can shift the calendar day. Prefer Intl.DateTimeFormat for display.
This JavaScript cheat sheet is written for people searching for a fast, accurate reference: students, junior developers, and teams shipping production software. Pin it, copy the snippets, and come back when syntax slips.
Quick reference
- Now:
Date.now()ms epoch.new Date()current instant. - ISO:
date.toISOString()always UTC withZ. Good for APIs. - Parse:
new Date(iso)for full ISO. Avoidnew Date("09/07/2026")— US vs local parsing. - Get:
getFullYear,getMonth(0–11),getDate(day of month). UTC variants exist. - Intl:
new Intl.DateTimeFormat("en-PK", { dateStyle: "medium", timeZone: "Asia/Karachi" }). - Compare: Compare timestamps or ISO strings, not formatted display text.
- Add days: Work in ms (
+ 86400000) or Temporal where available — DST makes “add 1 day” tricky. - Temporal: The newer Temporal API (where supported) models calendar dates separately from instants.
Copy-paste examples
Format in a known timezone
Do not split toISOString() and treat the UTC date as the user’s calendar day.
function formatKarachi(iso) {
return new Intl.DateTimeFormat('en-GB', {
dateStyle: 'medium',
timeStyle: 'short',
timeZone: 'Asia/Karachi',
}).format(new Date(iso));
}
function isSameLocalDay(a, b, timeZone) {
const fmt = new Intl.DateTimeFormat('en-CA', { timeZone, dateStyle: 'short' });
return fmt.format(a) === fmt.format(b);
}Common mistakes
getMonth()displayed without +1 so January is 0.- Parsing
"YYYY-MM-DD"and showing the previous evening in US timezones. - Storing display strings (
"07/09/2026") in JSON instead of ISO instants. - Using
setHours(0,0,0,0)for “today” in a server TZ that is not the user’s TZ.
FAQ
- Should I use Moment.js? No for new code. Use
IntlandDate, orTemporal/date-fns/luxonif you need a library.
- How do I send dates to PHP? ISO-8601 UTC (
toISOString()) or a Unix timestamp. Parse explicitly on the server.
- Why is my countdown off by an hour? DST. Adding 24 hours is not always adding a calendar day. Use timezone-aware libraries or Temporal for calendaring.
Related JavaScript cheat sheets
- Javascript Cheat Sheet 2026
- Javascript Array Methods Cheat Sheet
- Javascript String Methods Cheat Sheet
Build with this stack
When a cheat sheet is not enough — you need a production app, a student FYP, or a custom dashboard — ArpaNeuro builds custom web development and also sells ready-made source code. Request a quote and tell us the stack.
Browse software development services or the source code marketplace if you want a working codebase instead of starting from a blank file.