JS Date Objects

JS Date Objects

JS Date Objects

 

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:

  • getFullYear(): Returns the year of the date as a four-digit number.
  • getMonth(): Returns the month of the date as a number (0-11).
  • getDate(): Returns the day of the month of the date as a number (1-31).
  • getDay(): Returns the day of the week of the date as a number (0-6), where Sunday is 0 and Saturday is 6.
  • getHours(): Returns the hour of the date as a number (0-23).
  • getMinutes(): Returns the minutes of the date as a number (0-59).
  • getSeconds(): Returns the seconds of the date as a number (0-59).
  • getTime(): Returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

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:

  • toDateString(): Returns the date portion of the Date object as a human-readable string, without the time.
  • toTimeString(): Returns the time portion of the Date object as a human-readable string, without the date.
  • toLocaleDateString(): Returns the date portion of the Date object as a human-readable string, in the local date format.
  • toLocaleTimeString(): Returns the time portion of the Date object as a human-readable string, in the local time format.
  • toISOString(): Returns the date and time portion of the Date object as a human-readable string, in ISO format.

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:

  • getFullYear(): Returns the year of the date as a four-digit number (e.g. 2023).
  • getMonth(): Returns the month of the date as a number (0-11), where 0 is January and 11 is December.
  • getDate(): Returns the day of the month of the date as a number (1-31).
  • getDay(): Returns the day of the week of the date as a number (0-6), where 0 is Sunday and 6 is Saturday.
  • getHours(): Returns the hour of the date as a number (0-23).
  • getMinutes(): Returns the minutes of the date as a number (0-59).
  • getSeconds(): Returns the seconds of the date as a number (0-59).
  • getMilliseconds(): Returns the milliseconds of the date as a number (0-999).
  • getTime(): Returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • getTimezoneOffset(): Returns the time-zone offset from UTC, in minutes, for the current locale.

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:

  • setFullYear(): Sets the year of the date as a four-digit number (e.g. 2023).
  • setMonth(): Sets the month of the date as a number (0-11), where 0 is January and 11 is December.
  • setDate(): Sets the day of the month of the date as a number (1-31).
  • setHours(): Sets the hour of the date as a number (0-23).
  • setMinutes(): Sets the minutes of the date as a number (0-59).
  • setSeconds(): Sets the seconds of the date as a number (0-59).
  • setMilliseconds(): Sets the milliseconds of the date as a number (0-999).
  • setTime(): Sets the date and time of the Date object based on the number of milliseconds since January 1, 1970, 00:00:00 UTC.

 

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.

Join To Get Our Newsletter
Spread the love