date(JavaScript Variable data type)

Japanese version.

The Date object in JavaScript is used to represent dates and times. Dates in JavaScript are represented in milliseconds. Using the Date object makes it easy to work with and manipulate dates and times.

Contents

Code Example

Here is an example of using the Date object to get the current date and time:

const now = new Date();
console.log(now);

In this example, we create a Date object that represents the current date and time using the new Date() constructor, and assign it to the now variable. We then use the console.log() method to output the value of now to the console.

The Date object can represent dates and times in various formats. Using the methods of the Date object, you can get, add, or subtract dates and times. Here are some examples of common Date object methods:

const now = new Date();

// Get date
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();

// Get time
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();

In this example, we use the getFullYear(), getMonth(), getDate(), getHours(), getMinutes(), and getSeconds() methods of the Date object to get the current date and time.

The Date object in JavaScript is a powerful tool for working with dates and times in JavaScript programming.

Commonly used methods of the Date object

getDate(): Gets the day of the month for a specified date according to local time.

The getDate() method of the Date object returns an integer value between 1 and 31 that represents the day of the month for the date stored in the object.

For example, the following code gets the current date and displays the day of the month:

const today = new Date();
const dayOfMonth = today.getDate();

console.log(`Today is the ${dayOfMonth}th of the month.`);

This code creates a new Date object representing the current date, calls the getDate() method to get the day of the month, and then embeds that value in a string to display in the console.

When you run this code, it displays the day of the month for the current date.

getDay(): Gets the day of the week for a specified date according to local time. Returns 0 for Sunday, 1 for Monday, and so on.

The getDay() method of the Date object returns an integer value between 0 and 6 that represents the day of the week for the date stored in the object.

The method represents Sunday as 0, Monday as 1, Tuesday as 2, Wednesday as 3, Thursday as 4, Friday as 5, and Saturday as 6.

For example, the following code gets the current date and displays the day of the week:

const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const today = new Date();
const dayOfWeek = daysOfWeek[today.getDay()];

console.log(`Today is ${dayOfWeek}.`);

This code creates a new Date object representing the current date, calls the getDay() method to get the day of the week, and then retrieves the corresponding string from the daysOfWeek array. Finally, the code embeds the string in a template literal to display in the console.

When you run this code, it displays the day of the week for the current date.

getFullYear(): Gets the year of a date as a four digit number according to local time.

The getFullYear() method of the Date object returns a 4-digit integer representing the year of the date stored in the object.

For example, the following code gets the current date and displays the year:

const today = new Date();
const year = today.getFullYear();

console.log(`This year is ${year}.`);

This code creates a new Date object representing the current date, calls the getFullYear() method to get the year, and then embeds the value in a template literal to display in the console.

When you run this code, it displays the current year.

getHours(): Gets the hours of a date according to local time.

The getHours() method of the Date object returns an integer value between 0 and 23 representing the hour of the date stored in the object.

For example, the following code gets the current time and displays the hour:

const now = new Date();
const hours = now.getHours();

console.log(`It is now ${hours} o'clock.`);

This code creates a new Date object representing the current date and time, calls the getHours() method to get the hour value, and then embeds the value in a template literal to display in the console.

When you run this code, it displays the current hour.

getMilliseconds(): Gets the milliseconds of a date according to local time.

The getMilliseconds() method of the Date object returns an integer value between 0 and 999 representing the milliseconds of the date stored in the object.

For example, the following code gets the current time and displays the milliseconds:

const now = new Date();
const milliseconds = now.getMilliseconds();

console.log(`Milliseconds: ${milliseconds}`);

This code creates a new Date object representing the current date and time, calls the getMilliseconds() method to get the milliseconds value, and then embeds the value in a template literal to display in the console.

When you run this code, it displays the current milliseconds.

getMinutes(): Gets the minutes of a date according to local time.

The getMinutes() method of the Date object returns an integer value between 0 and 59 representing the minutes of the date stored in the object.

For example, the following code gets the current time and displays the minutes:

const now = new Date();
const minutes = now.getMinutes();

console.log(`Minutes: ${minutes}`);

This code creates a new Date object representing the current date and time, calls the getMinutes() method to get the minutes value, and then embeds the value in a template literal to display in the console.

When you run this code, it displays the current minutes.

getMonth(): Gets the month of a date according to local time. Returns 0 for January, 1 for February, and so on.

The getMonth() method of the Date object returns an integer value between 0 and 11 representing the month of the date stored in the object. 0 represents January, and 11 represents December.

For example, the following code gets the current time and displays the month:

const now = new Date();
const month = now.getMonth() + 1;

console.log(`Month: ${month}`);

This code creates a new Date object representing the current date and time, calls the getMonth() method to get the month value, adds 1 to the value to display the actual month (since the method returns a value between 0 and 11), and then embeds the value in a template literal to display in the console.

When you run this code, it displays the current month.

getSeconds(): Gets the seconds of a date according to local time.

The getSeconds() method of the Date object returns an integer value between 0 and 59 representing the seconds of the date stored in the object.

Here is an example that gets the current time and displays the seconds:

const now = new Date();
const seconds = now.getSeconds();

console.log(`Seconds: ${seconds}`);

This code creates a new Date object representing the current date and time, calls the getSeconds() method to get the seconds value, and then embeds the value in a template literal to display in the console.

When you run this code, it displays the current seconds.

getTime(): Gets the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.

The getTime() method of the Date object returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This number is also known as the Unix epoch time.

Here's an example that gets the current time and uses the getTime() method to get the Unix epoch time:

const now = new Date();
const epochTime = now.getTime();

console.log(`Epoch time: ${epochTime}`);

This code creates a new Date object representing the current date and time, calls the getTime() method to get the Unix epoch time, and then embeds the value in a template literal to display in the console.

When you run this code, it displays the current Unix epoch time.

getTimezoneOffset(): Gets the difference in minutes between the time on the local computer and UTC.

The getTimezoneOffset() method is a method of the Date object in JavaScript, which returns the difference in minutes between the local time and Coordinated Universal Time (UTC).

By using this method, you can determine the time difference between the local time and UTC of the browser. The value returned is a negative value if the local time is earlier than UTC, and a positive value if it is later.

Here's an example of using the getTimezoneOffset() method to determine the time difference between the browser's local time and UTC:

const now = new Date();
const offsetInMinutes = now.getTimezoneOffset();

console.log(`The browser's local time is ${offsetInMinutes} minutes behind UTC`);

This code creates a Date object representing the current date and time, uses the getTimezoneOffset() method to determine the time difference between the local time and UTC, and outputs the result to the console.

When you run this code, it displays the time difference in minutes between the browser's local time and UTC.

setDate(): Sets the day of the month for a specified date according to local time.

The setDate() method is used to set the date portion of a Date object. This method accepts as an argument an integer value from 1 to 31 representing the date to be set.

For example, the setDate() method can be used to obtain the date one day after the current date as follows

const now = new Date();
now.setDate(now.getDate() + 1);

console.log(now);

This code creates a Date object representing the current date and uses the getDate() method to retrieve the current date. Next, the setDate() method is used to set the date one day later. Finally, the updated Date object is output to the console.

The setDate() method automatically adjusts the month and year if the given date is out of range. For example, if you try to set the 31st, the date will be set to the end of the month.

const now = new Date('2022-04-30');
now.setDate(31);

console.log(now); // 2022-05-01

In this example, the setDate() method sets the 31st day of the month, but there are only 30 days in April. However, the setDate() method automatically adjusts the date to May 1.

setFullYear(): Sets the year of a date as a four digit number according to local time.

The setFullYear() method of the JavaScript Date object is used to set the year value. This method sets the year part of the Date object to the specified year.

The setFullYear() method can take one to three arguments:

  • If one argument is provided, it is interpreted as an integer value representing the year.
  • If two arguments are provided, the first argument is interpreted as an integer value representing the year, and the second argument is interpreted as an integer value representing the month.
  • If three arguments are provided, the first argument is interpreted as an integer value representing the year, the second argument is interpreted as an integer value representing the month, and the third argument is interpreted as an integer value representing the day.

Here's an example of using the setFullYear() method to set the year of a Date object:

// Get the current date
var currentDate = new Date();

// Set the year to 2022
currentDate.setFullYear(2022);

// Set the year, month, and day
currentDate.setFullYear(2023, 5, 1);

// Output the year
console.log(currentDate.getFullYear());

In this example, we get the current date, set the year to 2022 using the setFullYear() method, set the year, month, and day using another setFullYear() method, and output the year using the getFullYear() method.

setHours(): Sets the hours of a date according to local time.

The setHours() method of the JavaScript Date object is used to set the hour value. This method sets the hour part of the Date object to the specified time.

The setHours() method can take one to four arguments:

  • If one argument is provided, it is interpreted as an integer value representing the hour.
  • If two arguments are provided, the first argument is interpreted as an integer value representing the hour, and the second argument is interpreted as an integer value representing the minute.
  • If three arguments are provided, the first argument is interpreted as an integer value representing the hour, the second argument is interpreted as an integer value representing the minute, and the third argument is interpreted as an integer value representing the second.
  • If four arguments are provided, the first argument is interpreted as an integer value representing the hour, the second argument is interpreted as an integer value representing the minute, the third argument is interpreted as an integer value representing the second, and the fourth argument is interpreted as an integer value representing the millisecond.

Here's an example of using the setHours() method to set the time of a Date object:

// Get the current date
var currentDate = new Date();

// Set the hour to 10
currentDate.setHours(10);

// Set the hour and minute
currentDate.setHours(11, 30);

// Set the hour, minute, and second
currentDate.setHours(12, 45, 30);

// Set the hour, minute, second, and millisecond
currentDate.setHours(13, 15, 45, 500);

// Output the hour
console.log(currentDate.getHours());

In this example, we get the current date, set the hour to 10 using the setHours() method, set the hour and minute using another setHours() method, set the hour, minute, and second using another setHours() method, set the hour, minute, second, and millisecond using yet another setHours() method, and output the hour using the getHours() method.

setMilliseconds(): Sets the milliseconds of a date according to local time.

The setMilliseconds() method of the JavaScript Date object is used to set the millisecond value. This method sets the millisecond part of the Date object to the specified millisecond.

The setMilliseconds() method takes one argument, which must be an integer value representing the millisecond.

Here's an example of using the setMilliseconds() method to set the millisecond of a Date object:

// Get the current date
var currentDate = new Date();

// Set the millisecond to 500
currentDate.setMilliseconds(500);

// Output the millisecond
console.log(currentDate.getMilliseconds());

In this example, we get the current date, set the millisecond to 500 using the setMilliseconds() method, and output the millisecond using the getMilliseconds() method.

setMinutes(): Sets the minutes of a date according to local time.

The setMinutes() method of the JavaScript Date object is used to set the minute value. This method sets the minute part of the Date object to the specified minute.

The setMinutes() method can take one to three arguments:

  • If one argument is provided, it is interpreted as an integer value representing the minute.
  • If two arguments are provided, the first argument is interpreted as an integer value representing the minute, and the second argument is interpreted as an integer value representing the second.
  • If three arguments are provided, the first argument is interpreted as an integer value representing the minute, the second argument is interpreted as an integer value representing the second, and the third argument is interpreted as an integer value representing the millisecond.

Here's an example of using the setMinutes() method to set the minute of a Date object:

// Get the current date
var currentDate = new Date();

// Set the minute to 15
currentDate.setMinutes(15);

// Set the minute and second
currentDate.setMinutes(30, 45);

// Set the minute, second, and millisecond
currentDate.setMinutes(45, 30, 500);

// Output the minute
console.log(currentDate.getMinutes());

In this example, we get the current date, set the minute to 15 using the setMinutes() method, set the minute and second using another setMinutes() method, set the minute, second, and millisecond using yet another setMinutes() method, and output the minute using the getMinutes() method.

setMonth(): Sets the month of a date according to local time. Returns 0 for January, 1 for February, and so on.

The setMonth() method of the JavaScript Date object is used to set the month value. This method sets the month part of the Date object to the specified month.

The setMonth() method can take one or two arguments:

  • If one argument is provided, it is interpreted as an integer value representing the month. In this case, the value of the month is specified as a number between 0 and 11, where 0 represents January and 11 represents December.
  • If two arguments are provided, the first argument is interpreted as an integer value representing the month, and the second argument is interpreted as an integer value representing the day.

Here's an example of using the setMonth() method to set the month of a Date object:

// Get the current date
var currentDate = new Date();

// Set the month to February
currentDate.setMonth(1);

// Set the month and day
currentDate.setMonth(3, 15);

// Output the month
console.log(currentDate.getMonth());

In this example, we get the current date, set the month to February using the setMonth() method, set the month to April and the day to 15 using another setMonth() method, and output the month using the getMonth() method.

setSeconds(): Sets the seconds of a date according to local time.

The setSeconds() method of the JavaScript Date object is used to set the second value. This method sets the second part of the Date object to the specified second.

The setSeconds() method can take one or two arguments:

  • If one argument is provided, it is interpreted as an integer value representing the second.
  • If two arguments are provided, the first argument is interpreted as an integer value representing the second, and the second argument is interpreted as an integer value representing the millisecond.

Here's an example of using the setSeconds() method to set the second of a Date object:

// Get the current date
var currentDate = new Date();

// Set the second to 30
currentDate.setSeconds(30);

// Set the second and millisecond
currentDate.setSeconds(45, 500);

// Output the second
console.log(currentDate.getSeconds());

In this example, we get the current date, set the second to 30 using the setSeconds() method, set the second to 45 and the millisecond to 500 using another setSeconds() method, and output the second using the getSeconds() method.

setTime(): Sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.

The setTime() method of the JavaScript Date object is used to set the time portion of the date to a specified time. This method updates the Date object based on the value represented by the specified time in milliseconds.

The setTime() method takes a single argument, which must be a numeric value representing the new time that the Date object is to be set to, in milliseconds.

Here's an example of using the setTime() method to set the time of a Date object:

// Get the current date
var currentDate = new Date();

// Set the time to one minute later
currentDate.setTime(currentDate.getTime() + 60000);

// Output the time
console.log(currentDate);

In this example, we get the current date, use the getTime() method to get the current time in milliseconds, add one minute in milliseconds to the current time, and use the setTime() method to set the new time. Finally, we output the updated Date object to confirm the new time.

---

Links

JavaScript Articles