DateTime.roll

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

The difference between rolling and adding is that rolling does not affect larger units. Rolling a DateTime 12 months gets the exact same DateTime. However, the days can still be affected due to the differing number of days in each month.

Because there are no units larger than years, there is no difference between adding and rolling years.

  1. DateTime roll(long value, AllowDayOverflow allowOverflow)
    struct DateTime
    ref @safe pure nothrow @nogc
    roll
    (
    string units
    )
    if (
    units == "years" ||
    units == "months"
    )
  2. DateTime roll(long value)
  3. DateTime roll(long value)

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, 33, 33);
dt1.roll!"months"(1);
assert(dt1 == DateTime(2010, 2, 1, 12, 33, 33));

auto dt2 = DateTime(2010, 1, 1, 12, 33, 33);
dt2.roll!"months"(-1);
assert(dt2 == DateTime(2010, 12, 1, 12, 33, 33));

auto dt3 = DateTime(1999, 1, 29, 12, 33, 33);
dt3.roll!"months"(1);
assert(dt3 == DateTime(1999, 3, 1, 12, 33, 33));

auto dt4 = DateTime(1999, 1, 29, 12, 33, 33);
dt4.roll!"months"(1, AllowDayOverflow.no);
assert(dt4 == DateTime(1999, 2, 28, 12, 33, 33));

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

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

Meta