View source code
Display the source code in std/datetime/date.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.datetime.date.Date.roll - multiple declarations

Function Date.roll

Adds the given number of years or months to this Date, mutating it. 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

NameDescription
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.

Returns

A reference to the Date (this).

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, mutating it. A negative number will subtract.

ref Date roll(string units) (
  long days
) pure nothrow @nogc @safe
if (units == "days");

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

NameDescription
units The units to add. Must be "days".
days The number of days to add to this Date.

Returns

A reference to the Date (this).

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)

Authors

Jonathan M Davis

License

Boost License 1.0.