Tool
JavaScript Date Playground
Type a JavaScript Date expression and explore every output format and property in real time — toISOString, toString, getTime, getFullYear, and more.
The JavaScript Date object
JavaScript's built-in Date object stores time internally as the number of milliseconds since the Unix epoch (January 1, 1970 00:00:00 UTC). Every Date instance is ultimately just this single integer. The various methods — toISOString(), toString(), toLocaleString() — are different ways to display that same underlying value.
Common pitfalls when working with JavaScript dates
Several JavaScript Date behaviors trip up experienced developers:
- Months are zero-indexed: January is 0, December is 11 — always add 1 when displaying
- new Date('2024-01-01') parses as UTC midnight; new Date('2024/01/01') parses as local midnight
- new Date(2024, 0, 1) constructs January 1, 2024 in local time (month is 0-indexed)
- Date.now() returns milliseconds; Math.floor(Date.now() / 1000) gives Unix seconds
- Avoid adding days by adding 86400000ms — DST can cause this to land at the wrong time
Working with timezones in JavaScript
JavaScript's Date object has no timezone property — it is always stored in UTC. Timezone conversions are handled at display time using the Intl.DateTimeFormat API.
- new Intl.DateTimeFormat('en-US', { timeZone: 'Asia/Tokyo', dateStyle: 'full', timeStyle: 'long' }).format(date)
- date.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })
- new Intl.DateTimeFormat('en-CA', { timeZone: tz }).formatToParts(date) — returns individual parts for custom formatting
Frequently asked questions
- Why is getMonth() zero-indexed in JavaScript?
- JavaScript's getMonth() returns 0 for January and 11 for December, inherited from Java's original Date class. Always use date.getMonth() + 1 when displaying the month to users.
- What is the difference between new Date('2024-01-01') and new Date('2024/01/01')?
- new Date('2024-01-01') uses ISO 8601 format and is parsed as UTC midnight. new Date('2024/01/01') uses a non-standard format parsed as local midnight. Use ISO 8601 with an explicit timezone offset for predictable behavior.
- How do I format a Date in a specific timezone without a library?
- Use the built-in Intl.DateTimeFormat API: new Intl.DateTimeFormat('en-US', { timeZone: 'America/New_York', dateStyle: 'full', timeStyle: 'long' }).format(date). Supported in all modern browsers and Node.js 13+.