JavaScript Dates
JavaScript's `Date` object is used to handle dates and times. This guide covers date formats, methods to get date information, and methods to set date information.
1. Date Formats
JavaScript dates can be formatted in various ways. Here's how you can handle different date formats:
- ISO String
The ISO 8601 format is used to represent dates and times. It looks like `YYYY-MM-DDTHH:MM:SSZ`.
// Example of ISO string
let date = new Date();
console.log(date.toISOString()); // 2024-08-12T00:00:00.000Z
- Locale String
The locale string formats the date based on the locale of the user.
// Example of locale string
let date = new Date();
console.log(date.toLocaleDateString()); // 8/12/2024 (example format)
- Custom Formats
Custom formats can be created by extracting date components and concatenating them as needed.
// Example of custom format
let date = new Date();
let customFormat = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;
console.log(customFormat); // 12/8/2024
2. Get Date Methods
You can retrieve various components of a date using these methods:
- `getFullYear()`
Returns the year of the date as a four-digit number.
// Example of getFullYear()
let date = new Date();
console.log(date.getFullYear()); // 2024
- `getMonth()`
Returns the month of the date (0-11). January is 0, December is 11.
// Example of getMonth()
let date = new Date();
console.log(date.getMonth()); // 7 (August)
- `getDate()`
Returns the day of the month (1-31).
// Example of getDate()
let date = new Date();
console.log(date.getDate()); // 12
- `getDay()`
Returns the day of the week (0-6). Sunday is 0, Saturday is 6.
// Example of getDay()
let date = new Date();
console.log(date.getDay()); // 1 (Monday)
- `getHours()`
Returns the hour (0-23).
// Example of getHours()
let date = new Date();
console.log(date.getHours()); // 14 (2 PM)
- `getMinutes()`
Returns the minutes (0-59).
// Example of getMinutes()
let date = new Date();
console.log(date.getMinutes()); // 30
- `getSeconds()`
Returns the seconds (0-59).
// Example of getSeconds()
let date = new Date();
console.log(date.getSeconds()); // 45
3. Set Date Methods
You can set various components of a date using these methods:
- `setFullYear(year)`
Sets the year of the date.
// Example of setFullYear()
let date = new Date();
date.setFullYear(2025);
console.log(date.getFullYear()); // 2025
- `setMonth(month)`
Sets the month of the date (0-11).
// Example of setMonth()
let date = new Date();
date.setMonth(11); // December
console.log(date.getMonth()); // 11
- `setDate(day)`
Sets the day of the month (1-31).
// Example of setDate()
let date = new Date();
date.setDate(25);
console.log(date.getDate()); // 25
- `setHours(hours)`
Sets the hour (0-23).
// Example of setHours()
let date = new Date();
date.setHours(10);
console.log(date.getHours()); // 10
- `setMinutes(minutes)`
Sets the minutes (0-59).
// Example of setMinutes()
let date = new Date();
date.setMinutes(45);
console.log(date.getMinutes()); // 45
- `setSeconds(seconds)`
Sets the seconds (0-59).
// Example of setSeconds()
let date = new Date();
date.setSeconds(30);
console.log(date.getSeconds()); // 30