Differences between two dates (in days...)

Adam D. Ruppe destructionator at gmail.com
Thu Oct 24 12:01:21 UTC 2019


On Thursday, 24 October 2019 at 11:50:04 UTC, Mil58 wrote:
> See my script below, where I would like to do:
> "date of today" (var: auj) - "an older date" (var: deb) = xx 
> days

Try this:

Duration diff = cast(DateTime) auj - deb;
writeln("Diff : ", diff.total!"days");


So basically, just subtract them. You'll need them to both be 
SysTime (which has a timezone) or DateTime (which does not) - if 
you need to convert one, just use cast like I did there so they 
match.

The subtraction will return a Duration object. This has several 
methods to get units, or you can compare it directly

writeln(diff > 5.days); // legal

or like I did there, the `total` method gives the total number of 
units.

see the docs here

http://dpldocs.info/experimental-docs/core.time.Duration.html

and specifically for these methods there are examples which may 
be easier to read than the docs by themselves

http://dpldocs.info/experimental-docs/core.time.Duration.total.html
http://dpldocs.info/experimental-docs/core.time.Duration.split.html


More information about the Digitalmars-d-learn mailing list