Add a Month to a Date

Add a month to a Date

Vanilla R has a naive difftime class, but the Lubridate CRAN package lets you do what you ask:

require(lubridate)
d <- ymd(as.Date('2004-01-01')) %m+% months(1)
d
[1] "2004-02-01"

Hope that helps.

Add months to a date in Pandas

You could use pd.DateOffset

In [1756]: df.date + pd.DateOffset(months=plus_month_period)
Out[1756]:
0 2017-01-11
1 2017-02-01
Name: date, dtype: datetime64[ns]

Another way using pd.offsets.MonthOffset

In [1785]: df.date + pd.offsets.MonthOffset(plus_month_period)
Out[1785]:
0 2016-10-14
1 2016-11-04
Name: date, dtype: datetime64[ns]

Details

In [1757]: df
Out[1757]:
date
0 2016-10-11
1 2016-11-01

In [1758]: plus_month_period
Out[1758]: 3

CYPRESS: How to add one month to my current date with consideration to months like February(28 days) and months that have 30 days?

Both dayjs and javascript new Date() fail to add all the dates exactly as you want.

But you can use dayjs().daysInMonth() to get results exactly as per your description,

const getBilling = (startDate) => {
const [year, month, day] = startDate.split('/')

const sd = dayjs(startDate)
const firstOfNextMonth = sd.month(sd.month() + 1).date(1)
const daysInNextMonth = dayjs(firstOfNextMonth).daysInMonth()

let end;
if (daysInNextMonth < day) {
end = `${year}/${+month+2}/${1}` // always bump to 1st day of month + 2
} else {
end = `${year}/${+month+1}/${day}`
}

return dayjs(end, 'YYYY/MM/DD').format('YYYY/MM/DD')
}

it('gets billing date, accounting for short months', () => {

//Jan
expect(getBilling('2022/01/15')).to.eq('2022/02/15')
expect(getBilling('2022/01/31')).to.eq('2022/03/01')

//Feb
expect(getBilling('2022/02/15')).to.eq('2022/03/15')
expect(getBilling('2022/02/28')).to.eq('2022/03/28')

//Mar
expect(getBilling('2022/03/15')).to.eq('2022/04/15')
expect(getBilling('2022/03/31')).to.eq('2022/05/01')

})

Sample Image

How to add month to date in excel

Try below Date() formula.

=DATE(YEAR(B2),MONTH(B2)+A2,DAY(B2))

Sample Image

JavaScript function to add X months to a date

The following function adds months to a date in JavaScript (source). It takes into account year roll-overs and varying month lengths:

function addMonths(date, months) {    var d = date.getDate();    date.setMonth(date.getMonth() + +months);    if (date.getDate() != d) {      date.setDate(0);    }    return date;}
// Add 12 months to 29 Feb 2016 -> 28 Feb 2017console.log(addMonths(new Date(2016,1,29),12).toString());
// Subtract 1 month from 1 Jan 2017 -> 1 Dec 2016console.log(addMonths(new Date(2017,0,1),-1).toString());
// Subtract 2 months from 31 Jan 2017 -> 30 Nov 2016console.log(addMonths(new Date(2017,0,31),-2).toString());
// Add 2 months to 31 Dec 2016 -> 28 Feb 2017console.log(addMonths(new Date(2016,11,31),2).toString());

How to add months to a date in JavaScript?

Corrected as of 25.06.2019:

var newDate = new Date(date.setMonth(date.getMonth()+8));

Old
From here:

var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009 = jan312009.setMonth(jan312009.getMonth()+8);

Swift - Add N month to date

You can use Calendar method nextDate(after:) passing previousTimePreservingSmallerComponents as the matchingPolicy:

public extension Date {
func noon(using calendar: Calendar = .current) -> Date {
calendar.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
func day(using calendar: Calendar = .current) -> Int {
calendar.component(.day, from: self)
}
func adding(_ component: Calendar.Component, value: Int, using calendar: Calendar = .current) -> Date {
calendar.date(byAdding: component, value: value, to: self)!
}
func monthSymbol(using calendar: Calendar = .current) -> String {
calendar.monthSymbols[calendar.component(.month, from: self)-1]
}
}


var date = Date().noon()    // "Jun 30, 2020 at 12:00 PM"
let day = date.day()
let endDate = date.adding(.year, value: 2)
repeat {
date = Calendar.current.nextDate(after: date, matching: DateComponents(day: day, hour: 12), matchingPolicy: .previousTimePreservingSmallerComponents)!
print(date.description(with: .current), date.monthSymbol())
} while date <= endDate

this will print:

Thursday, July 30, 2020 at 12:00:00 PM Brasilia Standard Time July
Sunday, August 30, 2020 at 12:00:00 PM Brasilia Standard Time August
Wednesday, September 30, 2020 at 12:00:00 PM Brasilia Standard Time September
Friday, October 30, 2020 at 12:00:00 PM Brasilia Standard Time October
Monday, November 30, 2020 at 12:00:00 PM Brasilia Standard Time November
Wednesday, December 30, 2020 at 12:00:00 PM Brasilia Standard Time December
Saturday, January 30, 2021 at 12:00:00 PM Brasilia Standard Time January
Sunday, February 28, 2021 at 12:00:00 PM Brasilia Standard Time February
Tuesday, March 30, 2021 at 12:00:00 PM Brasilia Standard Time March
Friday, April 30, 2021 at 12:00:00 PM Brasilia Standard Time April
Sunday, May 30, 2021 at 12:00:00 PM Brasilia Standard Time May
Wednesday, June 30, 2021 at 12:00:00 PM Brasilia Standard Time June
Friday, July 30, 2021 at 12:00:00 PM Brasilia Standard Time July
Monday, August 30, 2021 at 12:00:00 PM Brasilia Standard Time August
Thursday, September 30, 2021 at 12:00:00 PM Brasilia Standard Time September
Saturday, October 30, 2021 at 12:00:00 PM Brasilia Standard Time October
Tuesday, November 30, 2021 at 12:00:00 PM Brasilia Standard Time November
Thursday, December 30, 2021 at 12:00:00 PM Brasilia Standard Time December
Sunday, January 30, 2022 at 12:00:00 PM Brasilia Standard Time January
Monday, February 28, 2022 at 12:00:00 PM Brasilia Standard Time February
Wednesday, March 30, 2022 at 12:00:00 PM Brasilia Standard Time March
Saturday, April 30, 2022 at 12:00:00 PM Brasilia Standard Time April
Monday, May 30, 2022 at 12:00:00 PM Brasilia Standard Time May
Thursday, June 30, 2022 at 12:00:00 PM Brasilia Standard Time June
Saturday, July 30, 2022 at 12:00:00 PM Brasilia Standard Time July

edit/update:

If you need every nth month:

var date = Date().noon()  // "Jun 30, 2020 at 12:00 PM"
let day = date.day()
let endDate = date.adding(.year, value: 2)
var dates: [Date] = []
let nthMonth = 3
var counter = 0
repeat {
counter += 1
date = Calendar.current.nextDate(after: date, matching: DateComponents(day: day, hour: 12), matchingPolicy: .previousTimePreservingSmallerComponents)!
if counter.isMultiple(of: nthMonth) {
dates.append(date)
print(date.description(with: .current), date.monthSymbol())
}
} while date <= endDate

This will print:

Wednesday, September 30, 2020 at 12:00:00 PM Brasilia Standard Time September
Wednesday, December 30, 2020 at 12:00:00 PM Brasilia Standard Time December
Tuesday, March 30, 2021 at 12:00:00 PM Brasilia Standard Time March
Wednesday, June 30, 2021 at 12:00:00 PM Brasilia Standard Time June
Thursday, September 30, 2021 at 12:00:00 PM Brasilia Standard Time September
Thursday, December 30, 2021 at 12:00:00 PM Brasilia Standard Time December
Wednesday, March 30, 2022 at 12:00:00 PM Brasilia Standard Time March
Thursday, June 30, 2022 at 12:00:00 PM Brasilia Standard Time June

How to properly add 1 month from now to current date in moment.js

var currentDate = moment('2015-10-30');
var futureMonth = moment(currentDate).add(1, 'M');
var futureMonthEnd = moment(futureMonth).endOf('month');

if(currentDate.date() != futureMonth.date() && futureMonth.isSame(futureMonthEnd.format('YYYY-MM-DD'))) {
futureMonth = futureMonth.add(1, 'd');
}

console.log(currentDate);
console.log(futureMonth);

DEMO

EDIT

moment.addRealMonth = function addRealMonth(d) {
var fm = moment(d).add(1, 'M');
var fmEnd = moment(fm).endOf('month');
return d.date() != fm.date() && fm.isSame(fmEnd.format('YYYY-MM-DD')) ? fm.add(1, 'd') : fm;
}

var nextMonth = moment.addRealMonth(moment());

DEMO

adding 1 month to a date

I have to work with dates in R, and one of the most useful packages that I found for date data is lubridate. For your problem, you can simply do the following:

require(lubridate)
# ymd function parses dates in year-month-day format
startDate <- ymd('2013-01-31')
# The %m+% adds months to dates without exceeding the last day
myDates <- startDate %m+% months(c(0:6))

lubridate also has many other functions for dates, and I highly recommend taking a look.



Related Topics



Leave a reply



Submit