Skip to content

JavaScript Date.setDate()

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

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: