Js Date Formats in JavaScript
? JavaScript Date Formats
Creating Dates in JavaScript
JavaScript uses the Date object to handle dates and times.
1. Creating Date Objects
const now = new Date(); // Current date and timeconst specificDate = new Date('2025-06-02T10:00:00'); // ISO 8601 formatconst fromParts = new Date(2025, 5, 2, 10, 0, 0); // Year, Month (0-based!), Day, Hours, Minutes, SecondsNote: Month is 0-indexed (January = 0, June = 5).
2. Common Date String Formats
| Format | Example | Description |
|---|---|---|
| ISO 8601 | "2025-06-02T10:00:00Z" | Standard format, best to use |
| Short Date | "06/02/2025" | Locale-dependent, US-style MM/DD/YYYY |
| Long Date | "June 2, 2025" | Human-readable |
| RFC 2822 | "Mon, 02 Jun 2025 10:00:00 GMT" | Email date format |
3. Date Methods for Formatting
.toISOString()— returns ISO 8601 string (UTC)
const now = new Date();console.log(now.toISOString()); // "2025-06-02T14:20:00.000Z".toDateString()— human-readable date only
console.log(now.toDateString()); // "Mon Jun 02 2025".toLocaleDateString()— locale-sensitive date string
console.log(now.toLocaleDateString('en-US')); // "6/2/2025"console.log(now.toLocaleDateString('en-GB')); // "02/06/2025"4. Parsing Dates
const date1 = new Date("2025-06-02");const date2 = Date.parse("2025-06-02T10:00:00Z"); // returns timestamp (ms)Tips
Prefer ISO 8601 format for consistency and cross-browser support.
Use libraries like date-fns or Moment.js for advanced formatting.
Want examples of formatting dates manually or using Intl.DateTimeFormat?