Why setDate ?
The JavaScript Date setDate () method is a built-in function. This metadata sets the day of the month for a specific date by local time.
Syntax:
Date.setDate(day)
- day (required): An integer representing the month of a specific date. The first day of the month is represented by 1, the last day is 31 or 0. If a month is 30 days, and we enter 33 as a value, we will not encounter an error. The setDate () method then takes 33 – 30 = 3. That is the third day of the next month.
return:
- A Number, representing the number of milliseconds between the date object and midnight January 1 1970
JavaScript setDate Examples
Example 1
JavaScript Date With the setDate () method, we can change the day at a specific date.
var date = new Date('April 21, 1983 22:47:55');
console.log(date.getDate());
date.setDate(14);
console.log(date.getDate());
21
14
Example 2
JavaScript Date The setDate () method can change today’s date.
var now = new Date();
console.log(now.getDate());
now.setDate(22);
console.log(now.getDate());
7
22
Example 3
The first day of the month is represented by 1, the last day is 31 or 0.
var date = new Date('October 21, 1983 09:22:12');
console.log(date.getDate());
date.setDate(0);
console.log(date.getDate());
21
30
Example 4
var date = new Date('October 21, 1983 09:22:12');
console.log(date.getDate());
date.setDate(34);
console.log(date.getDate());
3
Browser Support
| Chrome | yes |
| Edge | yes |
| Firefox | 1 |
| Internet Explorer | yes |
| Opera | yes |
| Safari | yes |
| Android webview | yes |
| Chrome for Android | yes |
| Edge mobile | yes |
| Firefox for Android | 4 |
| Opera Android | yes |
Leave a comment