std.datetime.date.Date.roll
- multiple declarations
Function Date.roll
Adds the given number of years or months to this Date
. A negative
number will subtract.
ref Date roll(string units)
(
long value,
AllowDayOverflow allowOverflow = AllowDayOverflow .yes
) pure nothrow @nogc @safe
if (units == "years");
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.
Parameters
Name | Description |
---|---|
units | The type of units to add ("years" or "months"). |
value | The number of months or years to add to this
Date . |
allowOverflow | Whether the day should be allowed to overflow, causing the month to increment. |
Example
auto d1 = Date(2010, 1, 1);
d1 .roll!"months"(1);
writeln(d1); // Date(2010, 2, 1)
auto d2 = Date(2010, 1, 1);
d2 .roll!"months"(-1);
writeln(d2); // Date(2010, 12, 1)
auto d3 = Date(1999, 1, 29);
d3 .roll!"months"(1);
writeln(d3); // Date(1999, 3, 1)
auto d4 = Date(1999, 1, 29);
d4 .roll!"months"(1, AllowDayOverflow .no);
writeln(d4); // Date(1999, 2, 28)
auto d5 = Date(2000, 2, 29);
d5 .roll!"years"(1);
writeln(d5); // Date(2001, 3, 1)
auto d6 = Date(2000, 2, 29);
d6 .roll!"years"(1, AllowDayOverflow .no);
writeln(d6); // Date(2001, 2, 28)
Function Date.roll
Adds the given number of units to this Date
. A negative number
will subtract.
The difference between rolling and adding is that rolling does not
affect larger units. For instance, rolling a Date
one
year's worth of days gets the exact same Date
.
The only accepted units are "days"
.
Parameters
Name | Description |
---|---|
units | The units to add. Must be "days" . |
days | The number of days to add to this Date . |
Example
auto d = Date(2010, 1, 1);
d .roll!"days"(1);
writeln(d); // Date(2010, 1, 2)
d .roll!"days"(365);
writeln(d); // Date(2010, 1, 26)
d .roll!"days"(-32);
writeln(d); // Date(2010, 1, 25)