Thread overview
Differences between two dates (in days...)
Oct 24, 2019
Mil58
Oct 24, 2019
Adam D. Ruppe
Oct 24, 2019
Mil58
Oct 24, 2019
Daniel Kozak
October 24, 2019
Hi All...
I am desperate for the answer to the following problem:
to obtain the difference between the date of today and an older date (results in days...)

See my script below, where I would like to do:
"date of today" (var: auj) - "an older date" (var: deb) = xx days

import std.stdio;
import std.datetime;
import core.time : Duration;

void main()
{
auto deb = DateTime(2019, 9, 5);
auto auj = Clock.currTime();
writeln("Aujourd'hui : ", auj.day, " ", auj.month, " ", auj.year);
writeln("Le début : ", deb.day, " ", deb.month, " ", deb.year);
}

Thanks in advance !  :-)
October 24, 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
October 24, 2019
On Thu, Oct 24, 2019 at 1:55 PM Mil58 via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>
> Hi All...
> I am desperate for the answer to the following problem:
> to obtain the difference between the date of today and an older
> date (results in days...)
>
> See my script below, where I would like to do:
> "date of today" (var: auj) - "an older date" (var: deb) = xx days
>
> import std.stdio;
> import std.datetime;
> import core.time : Duration;
>
> void main()
> {
> auto deb = DateTime(2019, 9, 5);
> auto auj = Clock.currTime();
> writeln("Aujourd'hui : ", auj.day, " ", auj.month, " ", auj.year);
> writeln("Le début : ", deb.day, " ", deb.month, " ", deb.year);
> }
>
> Thanks in advance !  :-)

import std.stdio;
import std.datetime;
import core.time : Duration;

void main()
{
auto deb = DateTime(2019, 9, 5);
auto auj = Clock.currTime();
writeln("Aujourd'hui : ", auj.day, " ", auj.month, " ", auj.year);
writeln("Le début : ", deb.day, " ", deb.month, " ", deb.year);
writeln("Diff in days : ", (cast(DateTime)auj - deb).total!"days");
}

October 24, 2019
On Thursday, 24 October 2019 at 12:01:21 UTC, Adam D. Ruppe wrote:
> On Thursday, 24 October 2019 at 11:50:04 UTC, Mil58 wrote:
>> [...]
>
> Try this:
>
> Duration diff = cast(DateTime) auj - deb;
> writeln("Diff : ", diff.total!"days");
>
> [...]

Awsome ! Thank you for your quick reply...
Both lines work perfectly  :-)