Days between dates: Difference between revisions
Remove bogus markup, Rearrange alphabetically, add perl 6 example
No edit summary |
Thundergnat (talk | contribs) (Remove bogus markup, Rearrange alphabetically, add perl 6 example) |
||
Line 1:
{{draft task
;Task:
Calculate the number of days between two dates. Date input should be of the form YYYY-MM-DD.
To demonstrate one of the numerous ways this can be done.
=={{header|Erlang}}==▼
<lang erlang>▼
-module(daysbetween).▼
-export([between/2,dateToInts/2]).▼
dateToInts(String,POS) ->▼
list_to_integer(lists:nth(POS,re:split(String ,"-",[{return,list},trim]))).▼
between(DateOne,DateTwo) ->▼
Y1 = dateToInts(DateOne ,1),▼
M1 = dateToInts(DateOne ,2),▼
D1 = dateToInts(DateOne ,3),▼
Y2 = dateToInts(DateTwo ,1),▼
M2 = dateToInts(DateTwo ,2),▼
D2 = dateToInts(DateTwo ,3),▼
GregOne = calendar:date_to_gregorian_days(Y1,M1,D1),▼
GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2),▼
GregTwo - GregOne.▼
</lang> ▼
{{out}}▼
erlang shell:▼
<pre>▼
30> c(daysbetween).▼
c(daysbetween).▼
{ok,daysbetween}▼
31> daysbetween:between("2019-01-01", "2019-09-30").▼
daysbetween:between("2019-01-01", "2019-09-30").▼
272▼
</pre>▼
=={{header|Perl 6}}==
Dates are first class objects in Perl 6 and may have arithmetic in days done directly on them.
<lang perl6>say Date.new('2019-09-30') - Date.new('2019-01-01');
say Date.new('2019-03-01') - Date.new('2019-02-01');
say Date.new('2020-03-01') - Date.new('2020-02-01');
say Date.new('2029-03-29') - Date.new('2019-03-29');
say Date.new('2019-01-01') + 90;
say Date.new('2020-01-01') + 90;
say Date.new('2019-02-29') + 30;
CATCH { default { .message.say; exit; } };</lang>
<pre>272
28
29
3653
2019-04-01
2020-03-31
Day out of range. Is: 29, should be in 1..28</pre>
=={{header|Python}}==
Line 46 ⟶ 105:
{{out}}
<pre>python days-between.py 2019-01-01 2019-09-30
▲272
▲</pre>
▲=={{header|Erlang}}==
▲<lang erlang>
▲-module(daysbetween).
▲-export([between/2,dateToInts/2]).
▲dateToInts(String,POS) ->
▲ list_to_integer(lists:nth(POS,re:split(String ,"-",[{return,list},trim]))).
▲between(DateOne,DateTwo) ->
▲ Y1 = dateToInts(DateOne ,1),
▲ M1 = dateToInts(DateOne ,2),
▲ D1 = dateToInts(DateOne ,3),
▲ Y2 = dateToInts(DateTwo ,1),
▲ M2 = dateToInts(DateTwo ,2),
▲ D2 = dateToInts(DateTwo ,3),
▲ GregOne = calendar:date_to_gregorian_days(Y1,M1,D1),
▲ GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2),
▲ GregTwo - GregOne.
▲</lang>
▲{{out}}
▲erlang shell:
▲<pre>
▲30> c(daysbetween).
▲c(daysbetween).
▲{ok,daysbetween}
▲31> daysbetween:between("2019-01-01", "2019-09-30").
▲daysbetween:between("2019-01-01", "2019-09-30").
272
</pre>
|