In JavaScript, the Date object is used to work with dates and times. It provides a way to represent a specific moment in time, and perform various operations on dates and times.
To create a new Date object, we can use one of the following syntax:
// Current
date and time
const currentDate
= new Date();
// Date and
time specified
const
specificDate = new Date(year, month, day, hours, minutes, seconds,
milliseconds);
The year, month, day, hours, minutes, seconds, and milliseconds parameters are optional, and if not specified, they will default to zero or the current time.
Once we have a Date object, we can use various methods to get or set different parts of the date and time. Some of the commonly used methods are:
For example, we can get the current year and month using the following code:
const
currentDate = new Date();
const
currentYear = currentDate.getFullYear();
const
currentMonth = currentDate.getMonth(); // returns 0 for January, 1 for February,
and so on.
We can also set different parts of the date and time using various methods such as setFullYear(), setMonth(), setDate(), setHours(), and so on.
JS Date Formats:
JavaScript provides several methods to format a Date object into a human-readable string representation. Some of the commonly used methods are:
For example, we can format the current date and time using the toLocaleString() method as follows:
const
currentDate = new Date();
const
formattedDate = currentDate.toLocaleString();
console.log(formattedDate);
// Example output: "3/24/2023, 9:30:45 AM"
We can also pass an optional argument to these methods to specify the format of the output string. For example, we can format the date portion of the Date object using the toLocaleDateString() method as follows:
const
currentDate = new Date();
const
formattedDate = currentDate.toLocaleDateString('en-US', { year: 'numeric',
month: 'long', day: 'numeric' });
console.log(formattedDate);
// Example output: "March 24, 2023"
In this example, we pass the en-US locale as the first argument to toLocaleDateString() to format the date in the US English format. We also pass an object as the second argument to specify the format of the output string. The year property specifies to include the year as a four-digit number, the month property specifies to include the full name of the month, and the day property specifies to include the day of the month as a number.
Get Date Methods:
In JavaScript, the Date object provides several methods to retrieve information about the date and time. Some of the commonly used methods are:
For example, we can get the current year, month, and day using the following code:
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const currentMonth = currentDate.getMonth(); // returns 0 for January, 1 for February, and so on.
const currentDay = currentDate.getDate();
We can also get the current time and timezone offset using the following code:
const currentDate = new Date();
const currentHours = currentDate.getHours();
const currentMinutes = currentDate.getMinutes();
const currentSeconds = currentDate.getSeconds();
const currentMilliseconds = currentDate.getMilliseconds();
const timezoneOffset = currentDate.getTimezoneOffset(); // returns the offset in minutes, e.g. -480 for Pacific Standard Time (PST)
Note that all of these methods are based on the local time zone of the computer running the JavaScript code. If you need to work with dates and times in different time zones, you may need to use additional libraries or APIs to convert between time zones.
Set Date Methods:
In addition to the get methods, the Date object in JavaScript also provides several methods to set the different parts of the date and time. Some of the commonly used methods are:
For example, we can set the date to March 31, 2023 at 2:30 PM using the following code:
const newDate = new Date();
newDate.setFullYear(2023);
newDate.setMonth(2); // Note that month is zero-indexed, so 2 represents March
newDate.setDate(31);
newDate.setHours(14); // 2:00 PM in 24-hour time
newDate.setMinutes(30);
newDate.setSeconds(0);
newDate.setMilliseconds(0);
We can also set the date and time based on the number of milliseconds since January 1, 1970, 00:00:00 UTC, as follows:
const newDate = new Date();
newDate.setTime(1657684200000); // March 31, 2023, 2:30 PM in milliseconds
Note that all of these methods modify the Date object in place and return the number of milliseconds since January 1, 1970, 00:00:00 UTC corresponding to the modified date and time.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.