HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

JavaScript Set Date Methods

In JavaScript, you can set different components of a Date object using various methods.


Here are some commonly used methods for setting date-related information:

  • setDate(day): Sets the day of the month (1-31).
  • setMonth(month): Sets the month (0-11).
  • setFullYear(year): Sets the year (4 digits).
  • setHours(hour): Sets the hour (0-23).
  • setMinutes(minute): Sets the minutes (0-59).
  • setSeconds(second): Sets the seconds (0-59).
  • setMilliseconds(ms): Sets the milliseconds (0-999).

setDate(day):

Sets the day of the month (1-31) for a specified date according to local time.

Example

currentDate.setDate(25);
 

setMonth(month):

Sets the month (0-11) for a specified date according to local time.

Example

currentDate.setMonth(11); // December (months are zero-based)
 

setFullYear(year):

Sets the year (optionally month and day) for a specified date according to local time.

Example

let currentDate = new Date();
currentDate.setFullYear(2024);
 

setHours(hour):

Sets the hour (0-23), minutes (0-59), seconds (0-59), and milliseconds (0-999) for a specified date according to local time.

Example

currentDate.setHours(12, 30, 0); // Set to 12:30:00
 

setMinutes(minute):

Sets the minutes (0-59), seconds (0-59), and milliseconds (0-999) for a specified date according to local time.

Example

currentDate.setMinutes(45, 0); // Set to XX:45:00
 

setSeconds(second)

Sets the seconds (0-59) and milliseconds (0-999) for a specified date according to local time.

Example

currentDate.setSeconds(30); // Set to XX:XX:30

setMilliseconds(ms)

Sets the milliseconds (0-999) for a specified date according to local time.

Example

currentDate.setMilliseconds(500); // Set to XX:XX:XX.500