View source code
Display the source code in std/datetime.d from which this page was generated on
github.
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.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Function std.datetime.Date.add
Adds the given number of years or months to this Date
. 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).
Prototype
ref Date add(string units)( long value, AllowDayOverflow allowOverflow = AllowDayOverflow.yes ) pure nothrow @safe if (units == "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.add!"months"(11); assert(d1 == Date(2010, 12, 1)); auto d2 = Date(2010, 1, 1); d2.add!"months"(-11); assert(d2 == Date(2009, 2, 1)); auto d3 = Date(2000, 2, 29); d3.add!"years"(1); assert(d3 == Date(2001, 3, 1)); auto d4 = Date(2000, 2, 29); d4.add!"years"(1, AllowDayOverflow.no); assert(d4 == Date(2001, 2, 28));
Authors
Jonathan M Davis and Kato Shoichi