Adding 1 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.

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.

How to properly add 1 month 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

Add 1 month to the date value

Recursive CTE

declare @tmp table (date date,month_id int);
insert into @tmp values('2014-11-30',1),('2014-11-30',2),('2014-11-30',3),('2014-11-30',4),('2014-11-30',5),('2014-11-30',6),('2015-01-01',1),('2015-01-01',2),('2015-01-01',3),('2015-01-01',4);

;with cte as
(
select date, month_id, DATEADD(MONTH, 1, date) as derived_date1 from @tmp where month_id = 1
union all select t.date, t.month_id, DATEADD(MONTH, 1, cte.derived_date1) from cte inner join @tmp t on cte.date = t.date and cte.month_id = t.month_id - 1
)
select * from cte order by date, month_id

In Javascript, Today(31/5) add 1 month will get 1/7

This is a weird way how dates work in JavaScript. According to documentation in MDN:

The current day of month will have an impact on the behaviour of this
method. Conceptually it will add the number of days given by the
current day of the month to the 1st day of the new month specified as
the parameter, to return the new date. For example, if the current
value is 31st August 2016, calling setMonth with a value of 1 will
return 2nd March 2016. This is because in 2016 February had 29 days.

In your case when you add a month to 31st of May you get 31st of June. This is not a valid date, and JavaScript translates it to 1st of July

PHP add month to date on a date with day of month 31

I think this should work -

$time = strtotime("2015-05-31");
echo $final = date("Y-m-d", strtotime("first day of next month", $time));

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());

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


Related Topics



Leave a reply



Submit