JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



JS Date Set Methods

JavaScript is a popular programming language that is used to create interactive web pages. One of the most important features of JavaScript is its ability to work with dates and times. The Date object in JavaScript provides a number of methods that allow you to manipulate dates and times. In this article, we will explore some of the most commonly used Date set methods in JavaScript.

Brief Explanation of JS Date Set Methods

The Date set methods in JavaScript allow you to set different parts of a date and time. These methods are used to modify the values of the Date object. The following are some of the most commonly used Date set methods in JavaScript:

  • setFullYear(): Sets the year of the date object.
  • setMonth(): Sets the month of the date object.
  • setDate(): Sets the day of the month of the date object.
  • setHours(): Sets the hour of the date object.
  • setMinutes(): Sets the minute of the date object.
  • setSeconds(): Sets the second of the date object.
  • setMilliseconds(): Sets the millisecond of the date object.
  • setTime(): Sets the time of the date object.

Code Examples

Let's take a look at some code examples to see how these Date set methods work:

setFullYear()

The setFullYear() method sets the year of the date object. Here's an example:


let date = new Date();
date.setFullYear(2022);
console.log(date.getFullYear()); // Output: 2022

setMonth()

The setMonth() method sets the month of the date object. Note that the month is zero-indexed, which means that January is 0 and December is 11. Here's an example:


let date = new Date();
date.setMonth(11);
console.log(date.getMonth()); // Output: 11

setDate()

The setDate() method sets the day of the month of the date object. Here's an example:


let date = new Date();
date.setDate(25);
console.log(date.getDate()); // Output: 25

setHours()

The setHours() method sets the hour of the date object. Here's an example:


let date = new Date();
date.setHours(12);
console.log(date.getHours()); // Output: 12

setMinutes()

The setMinutes() method sets the minute of the date object. Here's an example:


let date = new Date();
date.setMinutes(30);
console.log(date.getMinutes()); // Output: 30

setSeconds()

The setSeconds() method sets the second of the date object. Here's an example:


let date = new Date();
date.setSeconds(45);
console.log(date.getSeconds()); // Output: 45

setMilliseconds()

The setMilliseconds() method sets the millisecond of the date object. Here's an example:


let date = new Date();
date.setMilliseconds(500);
console.log(date.getMilliseconds()); // Output: 500

setTime()

The setTime() method sets the time of the date object. Here's an example:


let date = new Date();
date.setTime(1640995200000);
console.log(date.getTime()); // Output: 1640995200000

Conclusion

In this article, we explored some of the most commonly used Date set methods in JavaScript. These methods allow you to modify the values of a Date object and work with dates and times in your JavaScript code.

References

Activity