DateTime.add

Adds the given number of years or months to this DateTime, mutating it. A negative number will subtract.

Note that if day overflow is allowed, and the date with the adjusted year/month overflows the number of days in the new month, then the month will be incremented by one, and the day set to the number of days overflowed. (e.g. if the day were 31 and the new month were June, then the month would be incremented to July, and the new day would be 1). If day overflow is not allowed, then the day will be set to the last valid day in the month (e.g. June 31st would become June 30th).

struct DateTime
ref @safe pure nothrow @nogc
add
(
string units
)
if (
units == "years" ||
units == "months"
)

Parameters

units

The type of units to add ("years" or "months").

value long

The number of months or years to add to this DateTime.

allowOverflow AllowDayOverflow

Whether the days should be allowed to overflow, causing the month to increment.

Return Value

Type: DateTime

A reference to the DateTime (this).

Examples

auto dt1 = DateTime(2010, 1, 1, 12, 30, 33);
dt1.add!"months"(11);
assert(dt1 == DateTime(2010, 12, 1, 12, 30, 33));

auto dt2 = DateTime(2010, 1, 1, 12, 30, 33);
dt2.add!"months"(-11);
assert(dt2 == DateTime(2009, 2, 1, 12, 30, 33));

auto dt3 = DateTime(2000, 2, 29, 12, 30, 33);
dt3.add!"years"(1);
assert(dt3 == DateTime(2001, 3, 1, 12, 30, 33));

auto dt4 = DateTime(2000, 2, 29, 12, 30, 33);
dt4.add!"years"(1, AllowDayOverflow.no);
assert(dt4 == DateTime(2001, 2, 28, 12, 30, 33));

Meta