Days between dates: Difference between revisions
Content added Content deleted
(added erlang example) |
No edit summary |
||
Line 3: | Line 3: | ||
;Task: |
;Task: |
||
{{task | heading}} |
|||
Calculate the number of days between two dates. Date input should be |
Calculate the number of days between two dates. Date input should be of the form YYYY-MM-DD. |
||
The initial example in python is derived from discussion at: |
|||
⚫ | |||
This example is similar to one described on the following page as the origin based algorithm, there implemented in c: http://www.sunshine2k.de/articles/coding/datediffindays/calcdiffofdatesindates.html. |
|||
{{task heading|Motivation}} |
|||
To demonstrate one of the numerous ways this can be done. |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 19: | Line 17: | ||
''' Difference between two dates = g(y2,m2,d2) - g(y1,m1,d1) |
''' Difference between two dates = g(y2,m2,d2) - g(y1,m1,d1) |
||
Where g() gives us the Gregorian Calendar Day |
Where g() gives us the Gregorian Calendar Day |
||
Inspired by discussion at: |
|||
⚫ | |||
''' |
''' |
||
Revision as of 15:26, 30 September 2019
Days between dates is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Calculate the number of days between two dates. Date input should be of the form YYYY-MM-DD.
Motivation
To demonstrate one of the numerous ways this can be done.
Python
<lang python>
- !/usr/bin/python
import sys
Difference between two dates = g(y2,m2,d2) - g(y1,m1,d1)
Where g() gives us the Gregorian Calendar Day Inspired by discussion at: https://stackoverflow.com/questions/12862226/the-implementation-of-calculating-the-number-of-days-between-2-dates
def days( y,m,d ):
input year and month are shifted to begin the year in march m = (m + 9) % 12 y = y - m/10
with (m*306 + 5)/10 the number of days from march 1 to the current 'm' month result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result
def diff(one,two):
[y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') # strings to integers year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1
if __name__ == "__main__":
one = sys.argv[1] two = sys.argv[2] print diff(one,two)
</lang>
- Output:
python days-between.py 2019-01-01 2019-09-30 272
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>
- Output:
erlang shell:
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