Re: Differences between two dates (in days...)

2019-10-24 Thread Mil58 via Digitalmars-d-learn

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  :-)


Re: Differences between two dates (in days...)

2019-10-24 Thread Daniel Kozak via Digitalmars-d-learn
On Thu, Oct 24, 2019 at 1:55 PM Mil58 via Digitalmars-d-learn
 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");
}



Re: Differences between two dates (in days...)

2019-10-24 Thread Adam D. Ruppe via Digitalmars-d-learn

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


Differences between two dates (in days...)

2019-10-24 Thread Mil58 via Digitalmars-d-learn

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 !  :-)