Date.roll

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

The difference between rolling and adding is that rolling does not affect larger units. Rolling a Date 12 months gets the exact same Date. 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. Date roll(long value, AllowDayOverflow allowOverflow)
    struct Date
    @safe pure nothrow @nogc ref
    roll
    (
    string units
    )
    if (
    units == "years"
    )
  2. Date roll(long days)

Parameters

units

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

value long

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

allowOverflow AllowDayOverflow

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

Return Value

Type: Date

A reference to the Date (this).

Examples

auto d1 = Date(2010, 1, 1);
d1.roll!"months"(1);
assert(d1 == Date(2010, 2, 1));

auto d2 = Date(2010, 1, 1);
d2.roll!"months"(-1);
assert(d2 == Date(2010, 12, 1));

auto d3 = Date(1999, 1, 29);
d3.roll!"months"(1);
assert(d3 == Date(1999, 3, 1));

auto d4 = Date(1999, 1, 29);
d4.roll!"months"(1, AllowDayOverflow.no);
assert(d4 == Date(1999, 2, 28));

auto d5 = Date(2000, 2, 29);
d5.roll!"years"(1);
assert(d5 == Date(2001, 3, 1));

auto d6 = Date(2000, 2, 29);
d6.roll!"years"(1, AllowDayOverflow.no);
assert(d6 == Date(2001, 2, 28));

Meta