Understanding Date and Time in JavaScript
Practical guide to the JavaScript Date object: creating dates, timestamps, get/set methods, UTC vs local, parsing, formatting, and common gotchas.
Drake Nguyen
Founder · System Architect
Introduction
The javascript date object is the built-in API for working with dates and times in JavaScript. Whether you need to display a local timezone, calculate durations, schedule jobs, or filter items by opening hours, understanding this object and related javascript date methods is essential for reliable time handling in web apps.
The Date object: basics
Creating a new Date without arguments yields the current date and time according to the host system:
// current date and time
const now = new Date();
console.log(now); // e.g. 2026-02-24T12:34:56.789Z (format varies by environment)
Under the hood a Date stores a timestamp: the number of milliseconds since the Unix epoch (midnight UTC on 1 Jan 1970). You can retrieve that numeric value with javascript gettime:
// milliseconds since Epoch (Unix time in ms)
const ms = now.getTime();
console.log(ms); // e.g. 1677244496789
Creating dates (four common ways)
new Date()— current date and time.new Date(timestamp)— create from milliseconds since epoch (can be negative for pre-1970 dates).new Date(dateString)— parse a date string (be cautious: formats and timezone parsing can differ).new Date(year, monthIndex, day, hours, minutes, seconds, ms)— specify components; monthIndex is zero-based.
Example: create July 4, 1776 12:30 UTC in three ways:
// from timestamp (may be negative for dates before 1970)
new Date(-6106015800000);
// from ISO-like or human-readable string (parsing is implementation-dependent)
new Date('1776-07-04T12:30:00Z');
// from components (monthIndex: 6 = July)
new Date(1776, 6, 4, 12, 30, 0, 0);
Retrieving components with get methods
Use get methods to extract parts of a date in the local timezone. Common methods include:
getFullYear()— 4-digit year.getMonth()— monthIndex 0–11 (0 = January).getDate()— day of month 1–31.getDay()— day of week 0–6 (0 = Sunday).getHours(),getMinutes(),getSeconds(),getMilliseconds().getTime()— timestamp in milliseconds since epoch.
Example: getFullYear, getMonth and getDate:
const birthday = new Date(1980, 6, 31); // July 31, 1980 (monthIndex 6)
console.log(birthday.getFullYear()); // 1980
console.log(birthday.getMonth()); // 6
console.log(birthday.getDate()); // 31
Modifying dates with set methods
Each get method has a corresponding set method to change that component on an existing Date instance. Setting parts will update the internal timestamp accordingly.
// change the year
birthday.setFullYear(1997);
console.log(birthday); // now reflects year 1997
// set time from a timestamp
birthday.setTime(Date.now()); // set to current moment
Note: when you use setMonth(), supplying a value outside 0–11 will rollover months and adjust year as needed (e.g., month 12 becomes January of next year).
UTC methods vs local time
The get/set methods above use the local timezone. For timezone-independent logic, use the UTC variants (prefixed with getUTC or setUTC). These return or set values relative to Coordinated Universal Time (UTC).
const now = new Date();
console.log(now.getHours()); // local hour
console.log(now.getUTCHours()); // hour in UTC
Use UTC methods when you need consistent behavior across timezones, such as storing timestamps in a database or comparing events from multiple regions.
Parsing and formatting
Parsing date strings with new Date(dateString) works for ISO 8601 strings reliably in modern engines. For custom javascript date format output, use Intl.DateTimeFormat or libraries for complex formatting.
// parse ISO string
const d1 = new Date('2026-02-24T09:00:00Z');
// format using Intl (example)
const fmt = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'short', day: 'numeric', timeZone: 'UTC' });
console.log(fmt.format(d1)); // e.g. "Feb 24, 2026"
Quick tips and gotchas
- Month numbering starts at 0: January = 0, December = 11. Remember this when using
new Date(year, month, ...)orgetMonth(). - Negative timestamps represent dates before 1970 (javascript negative timestamp).
- Prefer
getTime()(milliseconds since epoch) for comparisons and arithmetic. - Be careful with locale-dependent date string parsing; ISO 8601 is safest for cross-browser behavior.
Examples: common tasks
- Create a date from a timestamp:
const fromTs = new Date(1609459200000); // Jan 1, 2021 00:00:00 UTC - Check if today is October 3rd:
const today = new Date(); if (today.getDate() === 3 && today.getMonth() === 9) { console.log("It's October 3rd."); } - Get milliseconds since epoch:
console.log(Date.now()); // faster shorthand for new Date().getTime()
Conclusion
The javascript date object provides flexible primitives for creating, reading, and modifying dates and times. Use timestamps (milliseconds since epoch) for arithmetic and storage, get/set methods to inspect or update components, and UTC methods to avoid timezone surprises. For formatting or advanced parsing, consider Intl APIs or a well-maintained library if your application requires complex date handling.
Further reading: consult the MDN Date reference and the ECMAScript specification for authoritative details on Date behavior across environments.