date
**Date and Time in JavaScript** JavaScript provides several ways to work with dates and times. Here are some of the most commonly used methods: ### Creating a Date Object You can create a date object using the `Date()` constructor: ```javascript const date = new Date(); console.log(date); // Output: current date and time ``` You can also create a date object from a string: ```javascript const date = new Date('2022-01-01T12:00:00'); console.log(date); // Output: January 1, 2022 12:00:00 ``` ### Getting Date and Time Components You can get individual components of a date object using the following methods: * `getFullYear()`: Returns the year of the date. * `getMonth()`: Returns the month of the date (0-11). * `getDate()`: Returns the day of the month (1-31). * `getHours()`: Returns the hour of the day (0-23). * `getMinutes()`: Returns the minute of the hour (0-59). * `getSeconds()`: Returns the second of the minute (0-59). ```javascript const date = new Date(); console.log(date.getFullYear()); // Output: current year console.log(date.getMonth() + 1); // Output: current month (1-12) console.log(date.getDate()); // Output: current day of the month console.log(date.getHours()); // Output: current hour (0-23) console.log(date.getMinutes()); // Output: current minute (0-59) console.log(date.getSeconds()); // Output: current second (0-59) ``` ### Setting Date and Time Components You can set individual components of a date object using the following methods: * `setFullYear()`: Sets the year of the date. * `setMonth()`: Sets the month of the date (0-11). * `setDate()`: Sets the day of the month (1-31). * `setHours()`: Sets the hour of the day (0-23). * `setMinutes()`: Sets the minute of the hour (0-59). * `setSeconds()`: Sets the second of the minute (0-59). ```javascript const date = new Date(); date.setFullYear(2022); date.setMonth(0); date.setDate(1); date.setHours(12); date.setMinutes(0); date.setSeconds(0); console.log(date); // Output: January 1, 2022 12:00:00 ``` ### Formatting Dates You can format dates using the `toLocaleString()` method: ```javascript const date = new Date(); console.log(date.toLocaleString()); // Output: current date and time in the user's locale ``` You can also use the `toISOString()` method to get a string representation of the date in ISO format: ```javascript const date = new Date(); console.log(date.toISOString()); // Output: current date and time in ISO format ``` ### Working with Time Zones JavaScript provides several ways to work with time zones: * `getTimezoneOffset()`: Returns the offset from UTC in minutes. * `getTimezone()`: Returns the time zone name. * `setTimezoneOffset()`: Sets the offset from UTC in minutes. * `setTimezone()`: Sets the time zone name. ```javascript const date = new Date(); console.log(date.getTimezoneOffset()); // Output: offset from UTC in minutes console.log(date.getTimezone()); // Output: time zone name date.setTimezoneOffset(-300); date.setTimezone('America/New_York'); console.log(date); // Output: date and time in the specified time zone ``` Note that working with time zones can be complex and requires careful consideration of daylight saving time (DST) and other factors.