Javascript - Date Set Methods

Introduction

Date Set methods are used for setting a part of a date.

MethodDescription
setDate() Set the day as a number (1-31)
setFullYear() Set the year (optionally month and day)
setHours()Set the hour (0-23)
setMilliseconds() Set the milliseconds (0-999)
setMinutes() Set the minutes (0-59)
setMonth()Set the month (0-11)
setSeconds() Set the seconds (0-59)
setTime() Set the time (milliseconds since January 1, 1970)

Demo

//setFullYear() sets a date object to a specific date. In this example, to January 14, 2020:
//The setFullYear() method sets a date object to a special date.
//Remember that JavaScript counts months from 0 to 11. Month 10 is November.

var d = new Date();
d.setFullYear(2020, 0, 14);//from  w  w  w  . j  a va 2 s  .  c o m
console.log(d);

//The setDate() Method
//setDate() sets the day of the month (1-31):
//The setDate() method sets the date of a month.
d.setDate(15);
console.log(d);

//The setDate() method can also be used to add days to a date:
d.setDate(d.getDate() + 50);
console.log(d);

Result

If adding days, shifts the month or year, the changes are handled automatically by the Date object.

Related Topic