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.DateTime.diffMonths

Returns the difference between the two DateTimes in months.

To get the difference in years, subtract the year property of two SysTimes. To get the difference in days or weeks, subtract the SysTimes themselves and use the core.time.Duration that results. Because converting between months and smaller units requires a specific date (which core.time.Durations don't have), getting the difference in months requires some math using both the year and month properties, so this is a convenience function for getting the difference in months.

Note that the number of days in the months or how far into the month either date is is irrelevant. It is the difference in the month property combined with the difference in years * 12. So, for instance, December 31st and January 1st are one month apart just as December 1st and January 31st are one month apart.

Prototype

int diffMonths(
  const(DateTime) rhs
) pure nothrow @safe const;

Parameters

NameDescription
rhs The DateTime to subtract from this one.

Example

assert(DateTime(1999, 2, 1, 12, 2, 3).diffMonths(
            DateTime(1999, 1, 31, 23, 59, 59)) == 1);

assert(DateTime(1999, 1, 31, 0, 0, 0).diffMonths(
            DateTime(1999, 2, 1, 12, 3, 42)) == -1);

assert(DateTime(1999, 3, 1, 5, 30, 0).diffMonths(
            DateTime(1999, 1, 1, 2, 4, 7)) == 2);

assert(DateTime(1999, 1, 1, 7, 2, 4).diffMonths(
            DateTime(1999, 3, 31, 0, 30, 58)) == -2);


Authors

Jonathan M Davis and Kato Shoichi

License

Boost License 1.0.

Comments