简体   繁体   中英

How to get next month same day on a given date in javascript (Not JQuery)

I'm currently working on calendar booking and if a client booked an event to occur on monthly basis but same day the first , then I will mark the calendar the same day next month based on the start booked date. Please see below for more details:

Example: given date = 2018-09-18 (3rd Tuesday) (start booked date) desired output dates: - (2018-10-16) (3rd Tuesday) - (2018-11-20) (3rd Tuesday)

Any idea? Thanks in advance

Edited:

Add this to you project once:

Date.prototype.AddMonth = function (i) {var dy=this.getDay(); this.setMonth(this.getMonth() + i * 1); this.setDate(this.getDate() - (this.getDay()-dy)); return this; };

now use it similar this everywhere:

(new Date()/*your date*/).AddMonth(1/*MonthCount*/);

New Approach (Using function):

If you don't want to use prototype, try this:

function AddMonth(date, i) {var dy=date.getDay(); date.setMonth(date.getMonth() + i * 1); date.setDate(date.getDate() - (date.getDay()-dy)); return date; };

Usage:

AddMonth(new Date("2018/09/18"), 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM