Days between dates: Difference between revisions

Added Quackery.
(→‎{{header|Ruby}}: Added Ruby)
(Added Quackery.)
 
(92 intermediate revisions by 48 users not shown)
Line 2:
 
;Task:
Calculate the number of days between two dates.
 
Calculate the number of days between two dates. Date input should be of the form   '''YYYY-MM-DD'''.
 
;Motivation
 
;Motivation
To demonstrate one of the numerous ways this can be done.
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F parse_date(date)
R time:strptime(date, ‘%Y-%m-%d’)
 
V date1 = parse_date(‘2019-01-01’)
V date2 = parse_date(‘2019-09-30’)
print((date2 - date1).days())</syntaxhighlight>
 
{{out}}
<pre>
272
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
TYPE Date=[CARD year BYTE month,day]
 
BYTE FUNC IsLeapYear(CARD y)
IF y MOD 100=0 THEN
IF y MOD 400=0 THEN RETURN (1)
ELSE RETURN (0) FI
FI
IF y MOD 4=0 THEN RETURN (1) FI
RETURN (0)
 
PROC DecodeDate(CHAR ARRAY s Date POINTER d)
BYTE ARRAY maxD=[0 31 28 31 30 31 30 31 31 30 31 30 31]
CHAR ARRAY tmp(5)
BYTE m
 
IF s(0)#10 THEN
Break()
FI
SCopyS(tmp,s,1,4) d.year=ValC(tmp)
SCopyS(tmp,s,6,7) d.month=ValB(tmp)
SCopyS(tmp,s,9,10) d.day=ValB(tmp)
 
IF d.year=0 OR d.month=0 OR d.month>12 OR d.day=0 THEN
Break()
FI
IF IsLeapYear(d.year) THEN
maxD(2)=29
ELSE
maxD(2)=28
FI
m=d.month
IF d.day>maxD(m) THEN
Break()
FI
RETURN
 
PROC DateToDays(Date POINTER d REAL POINTER res)
BYTE m
INT y,tmp1
REAL tmp2
 
m=(d.month+9) MOD 12
y=d.year-m/10
tmp1=y/4-y/100+y/400+(m*306+5)/10+(d.day-1)
 
IntToReal(365,res)
IntToReal(y,tmp2)
RealMult(res,tmp2,res)
IntToReal(tmp1,tmp2)
RealAdd(res,tmp2,res)
RETURN
 
PROC DiffDays(Date POINTER d1,d2 REAL POINTER diff)
REAL days1,days2
 
DateToDays(d1,days1)
DateToDays(d2,days2)
RealSub(days2,days1,diff)
RETURN
 
PROC Test(CHAR ARRAY s1,s2)
Date d1,d2
REAL diff
 
DecodeDate(s1,d1)
DecodeDate(s2,d2)
DiffDays(d1,d2,diff)
PrintF("%S .. %S -> ",s1,s2)
PrintRE(diff)
RETURN
 
PROC Main()
Put(125) PutE() ;clear the screen
Test("1995-11-21","1995-11-21")
Test("2019-01-01","2019-01-02")
Test("2019-01-02","2019-01-01")
Test("2019-01-01","2019-03-01")
Test("2020-01-01","2020-03-01")
Test("1902-01-01","1968-12-25")
Test("2090-01-01","2098-12-25")
Test("1902-01-01","2098-12-25")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Days_between_dates.png Screenshot from Atari 8-bit computer]
<pre>
1995-11-21 .. 1995-11-21 -> 0
2019-01-01 .. 2019-01-02 -> 1
2019-01-02 .. 2019-01-01 -> -1
2019-01-01 .. 2019-03-01 -> 59
2020-01-01 .. 2020-03-01 -> 60
1902-01-01 .. 1968-12-25 -> 24465
2090-01-01 .. 2098-12-25 -> 3280
1902-01-01 .. 2098-12-25 -> 71947
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Calendar;
with Ada.Text_IO;
with Ada.Integer_Text_IO;
 
with GNAT.Calendar.Time_IO;
 
procedure Days_Between_Dates is
 
function Days_Between (Lower_Date : in Ada.Calendar.Time;
Higher_Date : in Ada.Calendar.Time) return Integer
is
use Ada.Calendar;
Diff : constant Duration := Higher_Date - Lower_Date;
begin
return Integer (Diff / Day_Duration'Last);
end Days_Between;
 
procedure Put_Days_Between (Lower_Date : in String;
Higher_Date : in String;
Comment : in String)
is
use Ada.Text_IO;
use Ada.Integer_Text_IO;
use GNAT.Calendar.Time_IO;
 
Diff : constant Integer := Days_Between (Lower_Date => Value (Lower_Date),
Higher_Date => Value (Higher_Date));
begin
Put ("Days between " & Lower_Date & " and " & Higher_Date & " is ");
Put (Diff, Width => 5);
Put (" days -- ");
Put (Comment);
New_Line;
end Put_Days_Between;
 
begin
Put_Days_Between ("1995-11-21", "1995-11-21", "Identical dates");
Put_Days_Between ("2019-01-01", "2019-01-02", "Positive span");
Put_Days_Between ("2019-01-02", "2019-01-01", "Negative span");
Put_Days_Between ("2019-01-01", "2019-03-01", "Non-leap year");
Put_Days_Between ("2020-01-01", "2020-03-01", "Leap year");
Put_Days_Between ("1902-01-01", "1968-12-25", "Past");
Put_Days_Between ("2090-01-01", "2098-12-25", "Future");
Put_Days_Between ("1902-01-01", "2098-12-25", "Long span");
end Days_Between_Dates;</syntaxhighlight>
 
{{out}}
<pre>Days between 1995-11-21 and 1995-11-21 is 0 days -- Identical dates
Days between 2019-01-01 and 2019-01-02 is 1 days -- Positive span
Days between 2019-01-02 and 2019-01-01 is -1 days -- Negative span
Days between 2019-01-01 and 2019-03-01 is 59 days -- Non-leap year
Days between 2020-01-01 and 2020-03-01 is 60 days -- Leap year
Days between 1902-01-01 and 1968-12-25 is 24465 days -- Past
Days between 2090-01-01 and 2098-12-25 is 3280 days -- Future
Days between 1902-01-01 and 2098-12-25 is 71947 days -- Long span</pre>
 
=={{header|ALGOL 68}}==
{{Trans|FreeBASIC}}
<syntaxhighlight lang="algol68">
BEGIN # calculate the number of days between a pair of dates #
# based on a translation of the FreeBASIC sample #
 
[,]STRING test cases
= ( ( "1902-01-01", "1968-12-25" )
, ( "2019-01-01", "2019-01-02" ), ( "2019-01-02", "2019-01-01" )
, ( "2019-01-01", "2019-03-01" ), ( "2020-01-01", "2020-03-01" )
, ( "1995-11-21", "1995-11-21" ), ( "2090-01-01", "2098-12-25" )
);
 
PROC gregorian = ( INT y, m, d )INT:
BEGIN
INT n = ( m + 9 ) - ( ( ( m + 9 ) OVER 12 ) * 12 );
INT w = y - ( n OVER 10 );
( 365 * w ) + ( w OVER 4 ) - ( w OVER 100 ) + ( w OVER 400 )
+ ( ( ( n * 306 ) + 5 ) OVER 10 ) + ( d - 1 )
END # gregorian # ;
 
OP TOINT = ( STRING s )INT:
BEGIN
INT v := 0;
FOR s pos FROM LWB s TO UPB s DO
v *:= 10 +:= ( ABS s[ s pos ] - ABS "0" )
OD;
v
END # TOINT #;
 
FOR n FROM LWB test cases TO UPB test cases DO
STRING from date = test cases[ n, 1 ];
STRING to date = test cases[ n, 2 ];
INT from g = gregorian( TOINT from date[ 1 : 4 ]
, TOINT from date[ 6 : 7 ]
, TOINT from date[ 9 : 10 ]
);
INT to g = gregorian( TOINT to date[ 1 : 4 ]
, TOINT to date[ 6 : 7 ]
, TOINT to date[ 9 : 10 ]
);
print( ( "Days between ", from date, " and ", to date, " is " ) );
print( ( whole( to g - from g, -5 ), " days", newline ) )
OD
END
</syntaxhighlight>
{{out}}
<pre>
Days between 1902-01-01 and 1968-12-25 is 24465 days
Days between 2019-01-01 and 2019-01-02 is 1 days
Days between 2019-01-02 and 2019-01-01 is -1 days
Days between 2019-01-01 and 2019-03-01 is 59 days
Days between 2020-01-01 and 2020-03-01 is 60 days
Days between 1995-11-21 and 1995-11-21 is 0 days
Days between 2090-01-01 and 2098-12-25 is 3280 days
</pre>
 
=={{header|ALGOL W}}==
{{Trans|FreeBASIC}}
<syntaxhighlight lang="algolw">
begin % calculate the number of days between a pair of dates %
% based on a translation of the FreeBASIC sample %
 
integer procedure gregorian ( integer value y, m, d ) ;
begin
integer n, w;
n := ( m + 9 ) - ( ( ( m + 9 ) div 12 ) * 12 );
w := y - ( n div 10 );
( 365 * w ) + ( w div 4 ) - ( w div 100 ) + ( w div 400 )
+ ( ( ( n * 306 ) + 5 ) div 10 ) + ( d - 1 )
end gregorian ;
 
integer procedure toInt( string(4) value s ) ;
begin
integer v;
v := 0;
for sPos := 0 until 3 do begin
string(1) c;
c := s( sPos // 1 );
if c not = " " then v := ( v * 10 ) + ( decode( c ) - decode( "0" ) )
end;
v
end toInt ;
 
procedure testGregorian ( string(10) value fromDate, toDate ) ;
begin
integer fromG, toG;
fromG := gregorian( toInt( fromDate( 0 // 4 ) )
, toInt( fromDate( 5 // 2 ) )
, toInt( fromDate( 8 // 2 ) )
);
toG := gregorian( toInt( toDate( 0 // 4 ) )
, toInt( toDate( 5 // 2 ) )
, toInt( toDate( 8 // 2 ) )
);
writeon( s_w := 0, "Days between ", fromDate, " and ", toDate, " is " );
writeon( i_w := 5, s_w := 0, toG - fromG, " days" );
write();
end testGregorian ;
 
testGregorian( "1902-01-01", "1968-12-25" );testGregorian( "2019-01-01", "2019-01-02" );
testGregorian( "2019-01-02", "2019-01-01" );testGregorian( "2019-01-01", "2019-03-01" );
testGregorian( "2020-01-01", "2020-03-01" );testGregorian( "1995-11-21", "1995-11-21" );
testGregorian( "2090-01-01", "2098-12-25" )
 
end.
</syntaxhighlight>
{{out}}
<pre>
Days between 1902-01-01 and 1968-12-25 is 24465 days
Days between 2019-01-01 and 2019-01-02 is 1 days
Days between 2019-01-02 and 2019-01-01 is -1 days
Days between 2019-01-01 and 2019-03-01 is 59 days
Days between 2020-01-01 and 2020-03-01 is 60 days
Days between 1995-11-21 and 1995-11-21 is 0 days
Days between 2090-01-01 and 2098-12-25 is 3280 days
</pre>
 
=={{header|AppleScript}}==
 
<syntaxhighlight lang="applescript">on daysBetweenDates(date1, date2)
considering numeric strings -- Allows for leading zeros having been omitted.
if (date1 = date2) then return date1 & " and " & date2 & " are the same date"
end considering
-- Get the components of each date.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "-"
set {y1, m1, d1} to date1's text items
set {y2, m2, d2} to date2's text items
set AppleScript's text item delimiters to astid
-- Derive AppleScript date objects.
-- The best way to do this generally is to use the 'current date' function to obtain a date object
-- and then to set the properties of the result (or of a copy) to the required values.
-- The initial setting of the day values to 1 is to prevent overflow due to possibly
-- incompatible day and month values during the sequential setting of the properties.
-- The integer values set here are automatically coerced from the text values obtained above.
tell (current date) to set {its day, its year, its month, its day, its time, firstDate} to {1, y1, m1, d1, 0, it}
copy firstDate to secondDate
tell secondDate to set {its day, its year, its month, its day} to {1, y2, m2, d2}
-- Do the math.
set daysDifference to (firstDate - secondDate) div days
-- Format some output.
if (daysDifference > 0) then
return date1 & " comes " & daysDifference & " day" & item (((daysDifference is 1) as integer) + 1) of {"s", ""} & ¬
" after " & date2
else
return date1 & " comes " & -daysDifference & " day" & item (((daysDifference is -1) as integer) + 1) of {"s", ""} & ¬
" before " & date2
end if
end daysBetweenDates
 
return daysBetweenDates("2020-04-11", "2001-01-01") & linefeed & ¬
daysBetweenDates("2020-04-11", "2020-04-12") & linefeed & ¬
daysBetweenDates("2020-04-11", "2020-04-11")</syntaxhighlight>
 
{{output}}
<pre>"2020-04-11 comes 7040 days after 2001-01-01
2020-04-11 comes 1 day before 2020-04-12
2020-04-11 and 2020-04-11 are the same date"</pre>
 
 
Or, composing a function from reusable generics, and drawing on NSISO8601DateFormatter:
 
<syntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
 
 
-- daysBetween :: String -> String -> Int
on daysBetween(iso8601From, iso8601To)
set midnight to "T00:00:00+00:00"
((dateFromISO8601(iso8601To & midnight) - ¬
dateFromISO8601(iso8601From & midnight)) / 86400) as integer
end daysBetween
 
 
-- dateFromISO8601 :: String -> Date
on dateFromISO8601(isoDateString)
set ca to current application
tell ca's NSISO8601DateFormatter's alloc's init()
set its formatOptions to ¬
(ca's NSISO8601DateFormatWithInternetDateTime as integer)
(its dateFromString:(isoDateString)) as date
end tell
end dateFromISO8601
 
 
---------------------------TEST----------------------------
on run
script test
on |λ|(ab)
set {a, b} to ab
set delta to daysBetween(a, b)
(a & " -> " & b & " -> " & ¬
justifyRight(5, space, delta as string)) & ¬
pluralize(delta, " day")
end |λ|
end script
unlines(map(test, {¬
{"2020-04-11", "2001-01-01"}, ¬
{"2020-04-11", "2020-04-12"}, ¬
{"2020-04-11", "2020-04-11"}, ¬
{"2019-01-01", "2019-09-30"}}))
end run
 
 
 
--------------------GENERIC FUNCTIONS ---------------------
 
-- Absolute value.
-- abs :: Num -> Num
on abs(x)
if 0 > x then
-x
else
x
end if
end abs
 
 
-- justifyRight :: Int -> Char -> String -> String
on justifyRight(n, cFiller, strText)
if n > length of strText then
text -n thru -1 of ((replicate(n, cFiller) as text) & strText)
else
strText
end if
end justifyRight
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- pluralize :: Int -> String -> String
on pluralize(n, s)
set m to abs(n)
if 0 = m or 2 ≤ m then
s & "s"
else
s
end if
end pluralize
 
 
-- replicate :: Int -> String -> String
on replicate(n, s)
set out to ""
if n < 1 then return out
set dbl to s
repeat while (n > 1)
if (n mod 2) > 0 then set out to out & dbl
set n to (n div 2)
set dbl to (dbl & dbl)
end repeat
return out & dbl
end replicate
 
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set str to xs as text
set my text item delimiters to dlm
str
end unlines</syntaxhighlight>
{{Out}}
<pre>2020-04-11 -> 2001-01-01 -> -7040 days
2020-04-11 -> 2020-04-12 -> 1 day
2020-04-11 -> 2020-04-11 -> 0 days
2019-01-01 -> 2019-09-30 -> 272 days</pre>
 
=={{header|Arturo}}==
 
{{trans|Ruby}}
 
<syntaxhighlight lang="rebol">daysBetweenDates: function [startDate, endDate][
a: to :date.format: "dd/MM/yyyy" startDate
b: to :date.format: "dd/MM/yyyy" endDate
 
return abs b\days - a\days
]
 
print [
"days between the two dates:"
daysBetweenDates "01/01/2019" "19/10/2019"
]</syntaxhighlight>
 
{{out}}
 
<pre>days between the two dates: 291</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">db =
(
1995-11-21|1995-11-21
2019-01-01|2019-01-02
2019-01-02|2019-01-01
2019-01-01|2019-03-01
2020-01-01|2020-03-01
1902-01-01|1968-12-25
2090-01-01|2098-12-25
1902-01-01|2098-12-25
)
 
for i, line in StrSplit(db, "`n", "`r"){
D := StrSplit(line, "|")
result .= "Days between " D.1 " and " D.2 " : " Days_between(D.1, D.2) " Day(s)`n"
}
MsgBox, 262144, , % result
return
 
Days_between(D1, D2){
D1 := RegExReplace(D1, "\D")
D2 := RegExReplace(D2, "\D")
EnvSub, D2, % D1, days
return D2
}</syntaxhighlight>
{{out}}
<pre>Days between 1995-11-21 and 1995-11-21 : 0 Day(s)
Days between 2019-01-01 and 2019-01-02 : 1 Day(s)
Days between 2019-01-02 and 2019-01-01 : -1 Day(s)
Days between 2019-01-01 and 2019-03-01 : 59 Day(s)
Days between 2020-01-01 and 2020-03-01 : 60 Day(s)
Days between 1902-01-01 and 1968-12-25 : 24465 Day(s)
Days between 2090-01-01 and 2098-12-25 : 3280 Day(s)
Days between 1902-01-01 and 2098-12-25 : 71947 Day(s)</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f DAYS_BETWEEN_DATES.AWK
BEGIN {
Line 53 ⟶ 584:
return(result)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 84 ⟶ 615:
generic 2020-01-01 to 2020-03-01 = 60
</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">
#include<stdbool.h>
#include<string.h>
#include<stdio.h>
 
typedef struct{
int year, month, day;
}date;
 
date extractDate(char* str){
return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')};
}
 
bool isValidDate(char* str){
date newDate;
if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){
return false;
}
 
newDate = extractDate(str);
 
if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) ||
((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) ||
newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){
return false;
}
 
return true;
}
 
int diffDays(date date1,date date2){
int days1, days2;
date1.month = (date1.month + 9)%12;
date1.year = date1.year - date1.month/10;
 
date2.month = (date2.month + 9)%12;
date2.year = date2.year - date2.month/10;
 
days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 );
days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 );
 
return days2 - days1;
}
 
int main(int argc,char** argv)
{
if(argc!=3){
return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]);
}
 
if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){
return printf("Dates are invalid.\n");
}
 
printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2])));
 
return 0;
}
</syntaxhighlight>
Output :
<pre>
abhishek_ghosh@Azure:~/doodles$ ./a.out 2019-01-01 2019-12-02
Days Difference : 335
</pre>
 
=={{header|BQN}}==
 
The <code>date</code> namespace is taken from bqn-libs. <code>DBw</code> is the final function which calculates the proper difference between two date strings.
 
<syntaxhighlight lang="bqn">DivMod ← ⌊∘÷˜ ⋈ |
date ← {
o ← 719469
y ← 365.25
dur ← ⟨(100×y)-0.75, y, 30.6⟩
off ← ⟨o-0.25, 0.75, 0.41⟩
 
From ⇐ {
y‿m‿d ← 𝕩
f ← 0 > m -↩ 3
(d-o) +´ ⌊ off +⌾(¯1⊸⊑) dur × (100 DivMod y-f) ∾ ⟨m+12×f⟩
}
To ⇐ {
t←𝕩
c‿y‿m ← dur { d‿m ← 𝕨 DivMod 𝕩+t ⋄ t↩⌊m ⋄ d }¨ off
m -↩ 12×10≤m
⟨(100×c)+y+m<0, 3+m, 1+t⟩
}
}
Split←(⊢-˜+`׬)∘=⊔⊢
ToI←10⊸×⊸+˜´∘⌽-⟜'0'
 
S2D←ToI¨ '-'⊸Split
DBw ← -○(date.From S2D)</syntaxhighlight>
<pre> "2019-09-30" DBw "2019-01-01"
272</pre>
 
 
=={{header|C++}}==
{{trans|c}}
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
class Date {
private:
int year, month, day;
 
public:
Date(std::string str) {
if (isValidDate(str)) {
year = atoi(&str[0]);
month = atoi(&str[5]);
day = atoi(&str[8]);
} else {
throw std::exception("Invalid date");
}
}
 
int getYear() {
return year;
}
 
int getMonth() {
return month;
}
 
int getDay() {
return day;
}
 
// YYYY-MM-DD
static bool isValidDate(std::string str) {
if (str.length() != 10 || str[4] != '-' || str[7] != '-') {
return false;
}
 
if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) {
return false; // the year is not valid
}
if (!isdigit(str[5]) || !isdigit(str[6])) {
return false; // the month is not valid
}
if (!isdigit(str[8]) || !isdigit(str[9])) {
return false; // the day is not valid
}
 
int year = atoi(&str[0]);
int month = atoi(&str[5]);
int day = atoi(&str[8]);
 
// quick checks
if (year <= 0 || month <= 0 || day <= 0) {
return false;
}
if (month > 12) {
return false;
}
 
switch (month) {
case 2:
if (day > 29) {
return false;
}
if (!isLeapYear(year) && day == 29) {
return false;
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day > 31) {
return false;
}
break;
default:
if (day > 30) {
return false;
}
break;
}
 
return true;
}
 
static bool isLeapYear(int year) {
if (year > 1582) {
return ((year % 4 == 0) && (year % 100 > 0))
|| (year % 400 == 0);
}
if (year > 10) {
return year % 4 == 0;
}
// not bothering with earlier leap years
return false;
}
 
friend std::ostream &operator<<(std::ostream &, Date &);
};
 
std::ostream &operator<<(std::ostream &os, Date &d) {
os << std::setfill('0') << std::setw(4) << d.year << '-';
os << std::setfill('0') << std::setw(2) << d.month << '-';
os << std::setfill('0') << std::setw(2) << d.day;
return os;
}
 
int diffDays(Date date1, Date date2) {
int d1m = (date1.getMonth() + 9) % 12;
int d1y = date1.getYear() - d1m / 10;
 
int d2m = (date2.getMonth() + 9) % 12;
int d2y = date2.getYear() - d2m / 10;
 
int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1);
int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1);
 
return days2 - days1;
}
 
int main() {
std::string ds1 = "2019-01-01";
std::string ds2 = "2019-12-02";
 
if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) {
Date d1(ds1);
Date d2(ds2);
std::cout << "Days difference : " << diffDays(d1, d2);
} else {
std::cout << "Dates are invalid.\n";
}
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>Days difference : 335</pre>
 
===Alternative using Boost===
{{libheader|Boost}}
<syntaxhighlight lang="cpp">#include <cstdlib>
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
 
int main(int argc, char** argv) {
using namespace boost::gregorian;
if (argc != 3) {
std::cerr << "usage: " << argv[0] << " start-date end-date\n";
return EXIT_FAILURE;
}
try {
date start_date(from_simple_string(argv[1]));
date end_date(from_simple_string(argv[2]));
std::cout << end_date - start_date << '\n';
} catch (const std::exception& ex) {
std::cerr << ex.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}</syntaxhighlight>
 
{{out}}
<pre>
./days_between_dates 2020-01-01 2020-09-06
249
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
using System.Globalization;
 
public class Program
{
public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18"));
 
public static int DateDiff(string d1, string d2) {
var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture);
var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture);
return (int)(b - a).TotalDays;
}
}</syntaxhighlight>
{{out}}
<pre>
18187
</pre>
 
=={{header|COBOL}}==
{{works with|GnuCOBOL}}
 
<syntaxhighlight lang="cobol">COBOL *> days-between
*> Tectonics: cobc -xj days-between.cob
 
identification division.
program-id. days-between.
 
procedure division.
compute tally =
function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
-
function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
display tally
 
compute tally =
function integer-of-formatted-date('YYYYMMDD', '20191124')
-
function integer-of-formatted-date('YYYYMMDD', '20000101')
display tally
 
goback.
end program days-between.</syntaxhighlight>
 
{{out}}
<pre>prompt$ cobc -xj days-between-dates.cob
07267
07267</pre>
 
 
=={{header|Commodore BASIC}}==
<syntaxhighlight lang="gwbasic">
100 REM ===============================
110 REM DAYS BETWEEN 2 DATES
120 REM
130 REM CONVERT FROM TEXT TO PRG
140 REM USING C64LIST.EXE V3.5
150 REM COMMAND LINE UTILITY ON WINDOWS
160 REM
170 REM V1, 2021-10-09, ALVALONGO
180 REM ===============================
190 REM
200 REM INSPIRED BY THE PYTHON VERSION
210 REM OF THE ALGORITHM
220 REM AND THE DISCUSSION AT
230 REM HTTPS://STACKOVERFLOW.COM/QUESTIONS/12862226
240 REM /THE-IMPLEMENTATION-OF-CALCULATING-THE-NUMBER-OF-DAYS-BETWEEN-2-DATES
250 REM ===============================
260 :
1000 REM INIT ============================
1010 PRINT CHR$(147);:REM CLEAR SCREEN
1020 PRINT CHR$(5);:REM INK WHITE COLOR
1030 POKE 53280,3:REM BORDER COLOR CYAN
1040 POKE 53281,14:REM BACKGROUND COLOR BLUE
1050 :
1100 REM MAIN ==========================
1110 PRINT " DAYS BETWEEN 2 DATES"
1120 PRINT
1130 INPUT "FIRST DATE, YEAR";Y1
1140 INPUT "MONTH=";M1
1150 INPUT "DAY =";D1
1160 INPUT "SECOND DATE, YEAR";Y2
1170 INPUT "MONTH=";M2
1180 INPUT "DAY =";D2
1190 Y=Y1:M=M1:D=D1:GOSUB 9000:G1=G
1200 Y=Y2:M=M2:D=D2:GOSUB 9000:G2=G
1210 DI=ABS(G2-G1)
1220 PRINT "DAYS=";DI
1230 GET K$:IF K$="" THEN 1230
1240 PRINT
1250 GOTO 1120
1260 END
1270 :
9000 REM GREGORIAN ======================
9010 REM TRANSFORM A DATE INTO A DAY NUMBER IN THE GREGORIAN CALENDAR
9020 REM INPUT PARAMETERS: Y IS YEAR
9030 REM M IS MONTH
9040 REM D IS DAY
9050 N=(M+9)-INT((M+9)/12)*12
9060 W=Y-INT(N/10)
9070 G=365*W+INT(W/4)-INT(W/100)+INT(W/400)
9080 G=G+INT((N*306+5)/10)+(D-1)
9090 RETURN
</syntaxhighlight>
 
 
 
 
 
=={{header|D}}==
<syntaxhighlight lang="d">import std.datetime.date;
import std.stdio;
 
void main() {
auto fromDate = Date.fromISOExtString("2019-01-01");
auto toDate = Date.fromISOExtString("2019-10-07");
auto diff = toDate - fromDate;
writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days");
}</syntaxhighlight>
{{out}}
<pre>Number of days between 2019-Jan-01 and 2019-Oct-07: 279</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program Days_between_dates;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings;
begin
Result := TFormatSettings.Create();
with Result do
begin
DateSeparator := Delimiter;
ShortDateFormat := fmt;
end;
end;
 
function DaysBetween(Date1, Date2: string): Integer;
var
dt1, dt2: TDateTime;
fmt: TFormatSettings;
begin
fmt := CreateFormat('yyyy-mm-dd', '-');
dt1 := StrToDate(Date1, fmt);
dt2 := StrToDate(Date2, fmt);
Result := Trunc(dt2 - dt1);
end;
 
begin
Writeln(DaysBetween('1970-01-01', '2019-10-18'));
readln;
end.</syntaxhighlight>
{{out}}
<pre>18187</pre>
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">
 
-module(daysbetween).
Line 108 ⟶ 1,070:
 
</langsyntaxhighlight>
{{out}}
erlang shell:
Line 120 ⟶ 1,082:
</pre>
 
=={{header|Excel}}==
===LAMBDA===
 
Excel dates are numbers counting days since the start of January 1900, and arithmetic operations can be applied to them directly.
 
(Unless entered as strings, they differ from other numbers only in the display format applied to the containing cell).
 
The lambda expression below maps over an array of date strings, defining a new array of the differences, in units of one day, between today's date and those earlier dates.
 
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
 
{{Works with|Office 365 betas 2021}}
{{Out}}
 
The formula in cell B2 defines the array of day counts which populate the range '''B2:B5'''
 
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="4" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=LAMBDA(dateString, TODAY() - DATEVALUE(dateString) )(C2#)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
| D
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
|
| style="font-weight:bold" | Difference in days
| style="font-weight:bold" | Earlier date
| style="font-weight:bold" | Today
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
|
| style="text-align:right; background-color:#cbcefb" | 39020
| 1914-07-28
| style="text-align:right" | 2021-05-27
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| style="text-align:right" | 37088
| 1919-11-11
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| style="text-align:right" | 29916
| 1939-07-01
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| style="text-align:right" | 27661
| 1945-09-02
|
|}
 
Alternatively, we can express the same mapping over the whole '''C2#''' array by directly writing:
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="4" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|= TODAY() - DATEVALUE(C2#)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
| D
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
|
| style="font-weight:bold" | Difference in days
| style="font-weight:bold" | Earlier date
| style="font-weight:bold" | Today
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
|
| style="text-align:right; background-color:#cbcefb" | 39020
| 1914-07-28
| style="text-align:right" | 2021-05-27
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| style="text-align:right" | 37088
| 1919-11-11
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| style="text-align:right" | 29916
| 1939-07-01
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| style="text-align:right" | 27661
| 1945-09-02
|
|}
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Days between dates: Nigel Galloway. June 3rd., 2021
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31")
printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
</syntaxhighlight>
{{out}}
<pre>
There are 4847 days between 1792-9-22 and 1805-12-31
</pre>
=={{header|Factor}}==
Factor supports the addition and subtraction of timestamps and durations with the <code>time+</code> and <code>time-</code> words.
<langsyntaxhighlight lang="factor">USING: calendar calendar.parser kernel math prettyprint ;
 
: days-between ( ymd-str ymd-str -- n )
Line 128 ⟶ 1,201:
 
"2019-01-01" "2019-09-30" days-between .
"2016-01-01" "2016-09-30" days-between . ! leap year</langsyntaxhighlight>
{{out}}
<pre>
Line 134 ⟶ 1,207:
273
</pre>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Dim Shared As Integer M, Y, D
Dim As Integer Y1, M1, D1, Y2, M2, D2, G1, G2
Dim As String DaysBetween(7, 2) = {{"1902-01-01","1968-12-25"}, _
{"2019-01-01","2019-01-02"}, {"2019-01-02","2019-01-01"}, _
{"2019-01-01","2019-03-01"}, {"2020-01-01","2020-03-01"}, _
{"1995-11-21","1995-11-21"}, {"2090-01-01","2098-12-25"}}
 
Function Gregorian() As Integer
Dim As Integer N = (M+9) - Int((M+9)/12) * 12
Dim As Integer W = Y - Int(N/10)
Dim As Integer G = 365 * W + Int(W/4) - Int(W/100) + Int(W/400)
G += Int((N*306+5)/10)+(D-1)
Return G
End Function
 
For n As Integer = 0 To Ubound(DaysBetween)-1
Y1 = Val(Left(DaysBetween(n,0),4))
M1 = Val(Mid(DaysBetween(n,0),6,2))
D1 = Val(Right(DaysBetween(n,0),2))
Y2 = Val(Mid(DaysBetween(n,1),1,4))
M2 = Val(Mid(DaysBetween(n,1),6,2))
D2 = Val(Mid(DaysBetween(n,1),9,2))
Y = Y1 : M = M1 : D = D1 : G1 = Gregorian
Y = Y2 : M = M2 : D = D2 : G2 = Gregorian
Print "Days between "; DaysBetween(n,0); " and "; DaysBetween(n,1); " is ";
Print Using "##### days"; (G2-G1)
Next n
Sleep</syntaxhighlight>
{{out}}
<pre>
Days between 1902-01-01 and 1968-12-25 is 24465 days
Days between 2019-01-01 and 2019-01-02 is 1 days
Days between 2019-01-02 and 2019-01-01 is -1 days
Days between 2019-01-01 and 2019-03-01 is 59 days
Days between 2020-01-01 and 2020-03-01 is 60 days
Days between 1995-11-21 and 1995-11-21 is 0 days
Days between 2090-01-01 and 2098-12-25 is 3280 days
</pre>
 
=={{header|Frink}}==
Frink handles dates and intervals between dates robustly, including time zones. The sample below could use <CODE>minutes</CODE> or <CODE>seconds</CODE> instead of <CODE>days</CODE>.
 
Frink can even track leap seconds as they are added to the calendar if desired. The code <CODE>d1-d2</CODE> in the sample below could be replaced by <CODE>subtractLeap[d1,d2]</CODE> to obtain exact counts of seconds between dates.
 
See Frink's [https://frinklang.org/#DateTimeHandling Date/Time Handling] documentation to see how easy it is to work with dates, times, timezones, calendrical systems, and even leap seconds correctly and easily.
 
<syntaxhighlight lang="frink">d1 = # 2020-12-25 #
d2 = # 2020-12-06 #
println[d1-d2 -> days]</syntaxhighlight>
 
{{out}}
<pre>19
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?timeZone=America%2FLos_Angeles&script=examples/Days_between_dates}}
 
'''Solution'''
 
Note. For this script, the time zone is intentionally set to America/Los_Angeles, because it observes [https://en.wikipedia.org/wiki/Daylight_saving_time daylight saving time], It is necesary to solve this exercise.
 
Provided that the ToNumber expression applied to a time expression reduces to the number of milliseconds of such that time from the [https://en.wikipedia.org/wiki/Epoch_(computing) epoch]:
 
[[File:Fōrmulæ - Days between dates 01.png]]
 
[[File:Fōrmulæ - Days between dates 02.png]]
 
The solution seems easy, calculating the difference between two times (in milliseconds), and dividing by 86,4000,000 (number of milliseconds in a day):
 
[[File:Fōrmulæ - Days between dates 03.png]]
 
[[File:Fōrmulæ - Days between dates 04.png]]
 
However, it does not work if one time is in daylight saving time, and the other one is in standard time:
 
[[File:Fōrmulæ - Days between dates 05.png]]
 
[[File:Fōrmulæ - Days between dates 06.png]]
 
[[File:Fōrmulæ - Days between dates 07.png]]
 
[[File:Fōrmulæ - Days between dates 08.png]]
 
'''Solution 1'''
 
The first solution consists in simply rounding the result to the nearest integer:
 
[[File:Fōrmulæ - Days between dates 09.png]]
 
[[File:Fōrmulæ - Days between dates 10.png]]
 
'''Solution 2'''
 
The expression GetTimeZoneOffset reduces to the offset (in minutes) of the given time.
 
[[File:Fōrmulæ - Days between dates 11.png]]
 
[[File:Fōrmulæ - Days between dates 12.png]]
 
[[File:Fōrmulæ - Days between dates 13.png]]
 
[[File:Fōrmulæ - Days between dates 14.png]]
 
The solution consist in taking this difference in account.
 
So, the function that works correctly is:
 
[[File:Fōrmulæ - Days between dates 15.png]]
 
'''Test cases'''
 
[[File:Fōrmulæ - Days between dates 16.png]]
 
[[File:Fōrmulæ - Days between dates 17.png]]
 
Notice that it works even for fractions of days:
 
[[File:Fōrmulæ - Days between dates 18.png]]
 
[[File:Fōrmulæ - Days between dates 19.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 173 ⟶ 1,370:
days = daysBetween(date1, date2)
fmt.Printf("There are %d days between %s and %s\n", days, date1, date2)
}</langsyntaxhighlight>
 
{{out}}
Line 180 ⟶ 1,377:
There are 274 days between 2015-12-31 and 2016-09-30
</pre>
 
=={{header|Groovy}}==
{{trans|Kotlin}} {{trans|Java}}
<syntaxhighlight lang="groovy">import java.time.LocalDate
 
def fromDate = LocalDate.parse("2019-01-01")
def toDate = LocalDate.parse("2019-10-19")
def diff = fromDate - toDate
println "Number of days between ${fromDate} and ${toDate}: ${diff}"</syntaxhighlight>
{{out}}
<pre>Number of days between 2019-01-01 and 2019-10-19: 291</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
 
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
 
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
 
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
 
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date</syntaxhighlight>
{{out}}
<pre>There are 272 days between 2019-01-01 and 2019-09-30.
There are 274 days between 2015-12-31 and 2016-09-30.</pre>
 
Or, composing rather than raising errors:
 
<syntaxhighlight lang="haskell">import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (defaultTimeLocale, parseTimeM)
 
-------------------- DAYS BETWEEN DATES ------------------
 
daysBetween :: String -> String -> Maybe Integer
daysBetween s1 s2 =
dayFromString s2
>>= \d2 -> diffDays d2 <$> dayFromString s1
 
dayFromString :: String -> Maybe Day
dayFromString =
parseTimeM
True
defaultTimeLocale
"%Y-%-m-%-d"
 
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_
(putStrLn . uncurry showDateDiff)
[ ("2019-01-01", "2019-09-30"),
("2015-12-31", "2016-09-30")
]
 
showDateDiff :: String -> String -> String
showDateDiff s1 s2 =
maybe
(unlines ["Unparseable as date string pair:", s1, s2])
( \n ->
concat
[ "There are ",
show n,
" days between ",
s1,
" and ",
s2,
"."
]
)
$ daysBetween s1 s2</syntaxhighlight>
{{Out}}
<pre>There are 272 days between 2019-01-01 and 2019-09-30.
There are 274 days between 2015-12-31 and 2016-09-30.</pre>
 
=={{header|J}}==
<pre>
require'~addons/types/datetime/datetime.ijs'
2021 03 16 daysDiff 1960 05 04
22231
 
vectorize=: [: evaluate hyphen_to_space
hyphen_to_space=: (=&'-')`(,:&' ')}
evaluate=: ".
but_first=: &
 
 
'2021-03-16' daysDiff but_first vectorize '1960-05-04'
22231
</pre>
 
=={{header|Java}}==
{{trans|Kotlin}}
<syntaxhighlight lang="java">import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
 
public class DaysBetweenDates {
public static void main(String[] args) {
LocalDate fromDate = LocalDate.parse("2019-01-01");
LocalDate toDate = LocalDate.parse("2019-10-19");
long diff = ChronoUnit.DAYS.between(fromDate, toDate);
System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff);
}
}</syntaxhighlight>
{{out}}
<pre>Number of days between 2019-01-01 and 2019-10-19: 291</pre>
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">const timeRatio = 1000 * 60 * 60 * 24;
var floor = Math.floor, abs = Math.abs;
var daysBetween = (d1, d2) => floor(abs(new Date(d1) - new Date(d2)) / timeRatio);
 
console.log('Days between 2021-10-27 and 2020-03-03: %s', daysBetween('2021-10-27', '2020-03-03'));</syntaxhighlight>
{{out}}
<pre>Days between 2021-10-27 and 2020-03-03: 603</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
Two solutions are provided: the first uses jq's built-ins and is very brief.
The second starts from first principles,
and is of potential interest because of the various generic functions
that are provided. The output is the same in both cases.
 
===Using jq's built-ins===
<syntaxhighlight lang="jq">
def days_between(yyyymmddBefore; yyyymmddAfter):
(yyyymmddBefore | strptime("%Y-%m-%d") | mktime) as $before
| (yyyymmddAfter | strptime("%Y-%m-%d") | mktime) as $after
# leap seconds are always inserted
| (($after - $before) / (24*60*60) | floor) ;
 
def task:
def prolog: "In the following, if the dates are not the same,",
"the \"from\" date is included in the count of days, but the \"to\" date is not.\n",
"If the first date is later than the second date, the count of days is negated.";
 
def dates:
["1995-11-21", "1995-11-21"],
["2019-01-01", "2019-01-02"],
["2019-01-02", "2019-01-01"],
["2019-01-01", "2019-03-01"],
["2020-01-01", "2020-03-01"],
["1902-01-01", "1968-12-25"],
["2090-01-01", "2098-12-25"],
["1902-01-01", "2098-12-25"],
["1970-01-01", "2019-10-18"],
["2019-03-29", "2029-03-29"],
["2020-02-29", "2020-03-01"] ;
 
prolog,
(dates | "The number of days from \(.[0]) until \(.[1]) is \(days_between(.[0]; .[1] ))")
;
 
task
</syntaxhighlight>
{{out}}
<pre>
In the following, if the dates are not the same,
the "from" date is included in the count of days, but the "to" date is not.
 
If the first date is later than the second date, the count of days is negated.
The number of days from 1995-11-21 until 1995-11-21 is 0
The number of days from 2019-01-01 until 2019-01-02 is 1
The number of days from 2019-01-02 until 2019-01-01 is -1
The number of days from 2019-01-01 until 2019-03-01 is 59
The number of days from 2020-01-01 until 2020-03-01 is 60
The number of days from 1902-01-01 until 1968-12-25 is 24465
The number of days from 2090-01-01 until 2098-12-25 is 3280
The number of days from 1902-01-01 until 2098-12-25 is 71947
The number of days from 1970-01-01 until 2019-10-18 is 18187
The number of days from 2019-03-29 until 2029-03-29 is 3653
The number of days from 2020-02-29 until 2020-03-01 is 1
</pre>
 
===From first principles===
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang="jq">
# In general, dates should be valid Julian dates on or after Jan 1, 0001, but
# for the most part, this is not checked, in part because some
# computations based on ostensibly invalid dates do produce useful
# results, e.g. days(2000; 1; 1) computes the number of days from Jan 1, 0001
# up to and including Jan 1, 2000 whereas days(2000; 1; 0) excludes Jan 1, 2000.
 
# Output: the number of days from and including Jan 1, 0001,
# up to but excluding Jan 1 in the year $y for $y >= 1
def days_before:
if . < 1
then "The input to days_before should be a positive integer, not \(.)"|error
else (. - 1 | floor) as $y
| $y*365 + (($y/4)|floor) - (($y/100)|floor) + (($y/400)|floor)
end;
 
def isLeapYear:
.%4 == 0 and (.%100 != 0 or .%400 == 0);
 
# The day of the year (Jan 1 is 1)
def day_of_year($y; $m; $d):
def diy: [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365];
def diy2: [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366];
$d + if ($y|isLeapYear) then diy2[$m-1] else diy[$m-1] end;
 
# Output: the number of days from Jan 1, 0001 to the specified date, inclusive.
def days($y; $m; $d):
($y | days_before) + day_of_year($y; $m; $d);
 
# Output: the signed difference in the "days" values of the two dates.
# If the first specified date is later than the second specified date,
# then the result is the number of days from and including the earlier date,
# up to but excluding the later date.
def days_between(Year; Month; Day; laterYear; laterMonth; laterDay):
days(laterYear; laterMonth; laterDay) -
days(Year; Month; Day);
 
# Dates in yyyy-mm-dd format or as a numeric array [y,m,d]
def days_between(date; later):
def toa: if type == "string" then split("-") | map(tonumber) else . end;
(later | toa) as $later
| (date | toa) as $date
| days_between($date[0]; $date[1]; $date[2]; $later[0]; $later[1]; $later[2]);
</syntaxhighlight>
'''The Tasks'''
 
As above.
{{out}}
Same as above.
 
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Dates
 
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
</syntaxhighlight>{{out}}
<pre>
Day(DateTime("2019-09-30") - DateTime("2019-01-01")) = 272 days
Day(DateTime("2019-03-01") - DateTime("2019-02-01")) = 28 days
Day(DateTime("2020-03-01") - DateTime("2020-02-01")) = 29 days
Day(DateTime("2029-03-29") - DateTime("2019-03-29")) = 3653 days
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">import java.time.LocalDate
import java.time.temporal.ChronoUnit
 
fun main() {
val fromDate = LocalDate.parse("2019-01-01")
val toDate = LocalDate.parse("2019-10-19")
val diff = ChronoUnit.DAYS.between(fromDate, toDate)
println("Number of days between $fromDate and $toDate: $diff")
}</syntaxhighlight>
{{out}}
<pre>Number of days between 2019-01-01 and 2019-10-19: 291</pre>
 
=={{header|Lua}}==
This uses os.difftime to compare two Epoch times. Not to be used with dates before 1970.
<langsyntaxhighlight lang="lua">SECONDS_IN_A_DAY = 60 * 60 * 24
 
-- Convert date string as YYYY-MM-DD to Epoch time.
Line 197 ⟶ 1,664:
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")</langsyntaxhighlight>
{{out}}
<pre>Enter date 1: 1970-01-01
Enter date 2: 2019-10-02
There are 18171 days between these dates.</pre>
 
=={{header|M2000 Interpreter}}==
Version 12 has date type, so we can handle easy dates.
 
<syntaxhighlight lang="m2000 interpreter">
module Days_between_dates{
date a="2019-01-01", b="2019-09-30"
long z=b-a
// Use the system default to display dates (DD/MM/YYYY)
Print "Days from "+a+" to "+b+" = "+z
// using locale 1033 to display dates (MM/DD/YYYY)
Print "Days from "+date$(a, 1033)+" to "+date$(b, 1033)+" = "+z
}
Days_between_dates
</syntaxhighlight>
{{out}}
<pre>
Days from 1/1/2019 to 30/9/2019 = 272
Days from 1/1/2019 to 9/30/2019 = 272
</pre>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]</syntaxhighlight>
{{out}}
<pre>Quantity[60, "Days"]
Quantity[59, "Days"]</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import times
 
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
 
const Dates = [("1995-11-21","1995-11-21"),
("2019-01-01","2019-01-02"),
("2019-01-02","2019-01-01"),
("2019-01-01","2019-03-01"),
("2020-01-01","2020-03-01"),
("1902-01-01","1968-12-25"),
("2090-01-01","2098-12-25")]
 
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)</syntaxhighlight>
 
{{out}}
<pre>Days between 1995-11-21 and 1995-11-21: 0
Days between 2019-01-01 and 2019-01-02: 1
Days between 2019-01-02 and 2019-01-01: -1
Days between 2019-01-01 and 2019-03-01: 59
Days between 2020-01-01 and 2020-03-01: 60
Days between 1902-01-01 and 1968-12-25: 24465
Days between 2090-01-01 and 2098-12-25: 3280</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Program DaysBetweenDates;
{$mode ObjFPC}{$H+}
 
Uses dateutils,strutils;
 
Type Tarr = array of array Of string;
 
Const lst : Tarr = (('1902-01-01','1968-12-25'),('2019-01-01','2019-01-02'),
('2019-01-02','2019-01-01'),('2019-01-01','2019-03-01'),
('2020-01-01','2020-03-01'),('1995-11-21','1995-11-21'),
('2090-01-01','2098-12-25'),('1967-02-23','2024-03-21'));
 
Function strtodate(str : String) : tdatetime;
Begin
result := ScanDateTime('YYYYMMDD', DelChars(str, '-'));
End;
 
Var arr : array of string;
DaysBetw : integer;
Begin
For arr In lst Do
Begin
DaysBetw := DaysBetween(strtodate(arr[0]),strtodate(arr[1]));
writeln(arr[0],' - ',arr[1],' -> ',DaysBetw);
End;
End.
</syntaxhighlight>
{{out}}
<pre>
1902-01-01 - 1968-12-25 -> 24465
2019-01-01 - 2019-01-02 -> 1
2019-01-02 - 2019-01-01 -> 1
2019-01-01 - 2019-03-01 -> 59
2020-01-01 - 2020-03-01 -> 60
1995-11-21 - 1995-11-21 -> 0
2090-01-01 - 2098-12-25 -> 3280
1967-02-23 - 2024-03-21 -> 20846
</pre>
 
=={{header|Perl}}==
Would not reinvent this wheel.
<langsyntaxhighlight lang="perl">use feature 'say';
use Date::Calc qw(Delta_Days);
 
Line 212 ⟶ 1,776:
say Delta_Days(2000,1,1, 2100,1,1); # another, with one extra leap day
say Delta_Days(2020,1,1, 2019,10,1); # backwards in time
say Delta_Days(2019,2,29, 2019,3,1); # croaks</langsyntaxhighlight>
{{out}}
<pre>427
Line 220 ⟶ 1,784:
Date::Calc::PP::Delta_Days(): Date::Calc::Delta_Days(): not a valid date at Days_between_dates line 10</pre>
 
=={{header|Perl 6Phix}}==
{{libheader|Phix/basics}}
Dates are first class objects in Perl 6 and may have arithmetic in days done directly on them.
<!--<syntaxhighlight lang="phix">-->
<lang perl6>say Date.new('2019-09-30') - Date.new('2019-01-01');
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins<span style="color: #0000FF;">\<span style="color: #004080;">timedate<span style="color: #0000FF;">.<span style="color: #000000;">e</span>
<span style="color: #000080;font-style:italic;">-- specify as many or as few permitted formats as you like:</span>
<span style="color: #7060A8;">set_timedate_formats<span style="color: #0000FF;">(<span style="color: #0000FF;">{<span style="color: #008000;">"YYYY-MM-DD"<span style="color: #0000FF;">,<span style="color: #008000;">"DD/MM/YYYY"<span style="color: #0000FF;">,<span style="color: #008000;">"YYYY/MM/DD"<span style="color: #0000FF;">}<span style="color: #0000FF;">)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">SECONDS_TO_DAYS</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">60<span style="color: #0000FF;">*<span style="color: #000000;">60<span style="color: #0000FF;">*<span style="color: #000000;">24</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test<span style="color: #0000FF;">(<span style="color: #004080;">string</span> <span style="color: #000000;">d1<span style="color: #0000FF;">,</span> <span style="color: #000000;">d2<span style="color: #0000FF;">,</span> <span style="color: #000000;">desc<span style="color: #0000FF;">=<span style="color: #008000;">""<span style="color: #0000FF;">)</span>
<span style="color: #004080;">timedate</span> <span style="color: #000000;">td1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">parse_date_string<span style="color: #0000FF;">(<span style="color: #000000;">d1<span style="color: #0000FF;">)<span style="color: #0000FF;">,</span>
<span style="color: #000000;">td2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">parse_date_string<span style="color: #0000FF;">(<span style="color: #000000;">d2<span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">timedate_diff<span style="color: #0000FF;">(<span style="color: #000000;">td1<span style="color: #0000FF;">,<span style="color: #000000;">td2<span style="color: #0000FF;">,<span style="color: #000000;">DT_DAY<span style="color: #0000FF;">)<span style="color: #0000FF;">,</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s<span style="color: #0000FF;">/<span style="color: #000000;">SECONDS_TO_DAYS</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">elapsed<span style="color: #0000FF;">(<span style="color: #000000;">s<span style="color: #0000FF;">)<span style="color: #0000FF;">&<span style="color: #000000;">desc</span>
<span style="color: #7060A8;">printf<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"Days between %s and %s: %d [%s]\n"<span style="color: #0000FF;">,<span style="color: #0000FF;">{<span style="color: #000000;">d1<span style="color: #0000FF;">,<span style="color: #000000;">d2<span style="color: #0000FF;">,<span style="color: #000000;">d<span style="color: #0000FF;">,<span style="color: #000000;">e<span style="color: #0000FF;">}<span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">test<span style="color: #0000FF;">(<span style="color: #008000;">"1969-12-31"<span style="color: #0000FF;">,<span style="color: #008000;">"1970-01-01"<span style="color: #0000FF;">)</span>
<span style="color: #000000;">test<span style="color: #0000FF;">(<span style="color: #008000;">"1995-11-21"<span style="color: #0000FF;">,<span style="color: #008000;">"1995-11-21"<span style="color: #0000FF;">,<span style="color: #008000;">", same date"<span style="color: #0000FF;">)</span>
<span style="color: #000000;">test<span style="color: #0000FF;">(<span style="color: #008000;">"2019-01-02"<span style="color: #0000FF;">,<span style="color: #008000;">"2019-01-01"<span style="color: #0000FF;">,<span style="color: #008000;">", negative date"<span style="color: #0000FF;">)</span>
<span style="color: #000000;">test<span style="color: #0000FF;">(<span style="color: #008000;">"2019-01-01"<span style="color: #0000FF;">,<span style="color: #008000;">"2019-03-01"<span style="color: #0000FF;">,<span style="color: #008000;">", non-leap year"<span style="color: #0000FF;">)</span>
<span style="color: #000000;">test<span style="color: #0000FF;">(<span style="color: #008000;">"2020-01-01"<span style="color: #0000FF;">,<span style="color: #008000;">"2020-03-01"<span style="color: #0000FF;">,<span style="color: #008000;">", leap year"<span style="color: #0000FF;">)</span>
<span style="color: #000000;">test<span style="color: #0000FF;">(<span style="color: #008000;">"1970-01-01"<span style="color: #0000FF;">,</span> <span style="color: #008000;">"2019/10/18"<span style="color: #0000FF;">)</span>
<span style="color: #000000;">test<span style="color: #0000FF;">(<span style="color: #008000;">"1970-01-01"<span style="color: #0000FF;">,</span> <span style="color: #008000;">"18/10/2019"<span style="color: #0000FF;">)
<!--</syntaxhighlight>-->
As shown, timedate_diff() can optionally round to the nearest whole number of days [else omit DT_DAY].<br>
Note that elapsed() assumes all years are exactly 365 days, and in no way takes leap years into consideration
(as opposed, of course, to timedate_diff() which handles them flawlessly), not that you sh/c/would ever use the
string output of elapsed() in any further calculations anyway.
{{out}}
<pre>
Days between 1969-12-31 and 1970-01-01: 1 [1 day]
Days between 1995-11-21 and 1995-11-21: 0 [0s, same date]
Days between 2019-01-02 and 2019-01-01: -1 [minus 1 day, negative date]
Days between 2019-01-01 and 2019-03-01: 59 [59 days, non-leap year]
Days between 2020-01-01 and 2020-03-01: 60 [60 days, leap year]
Days between 1970-01-01 and 2019/10/18: 18187 [49 years, 302 days]
Days between 1970-01-01 and 18/10/2019: 18187 [49 years, 302 days]
</pre>
 
=={{header|PicoLisp}}==
say Date.new('2019-03-01') - Date.new('2019-02-01');
<syntaxhighlight lang="picolisp">(de diffDates (A B)
(abs (- ($dat A "-") ($dat B "-"))) )
(println (diffDates "2019-1-1" "2019-9-30"))
(println (diffDates "2015-12-31" "2016-09-30"))</syntaxhighlight>
{{out}}
<pre>
272
274
</pre>
 
=={{header|PL/I-80}}==
say Date.new('2020-03-01') - Date.new('2020-02-01');
{{Trans|S-BASIC}}
<syntaxhighlight lang = "PL/I">
elapsed_days: proc options (main);
dcl
(date1, date2) float bin,
another char(1);
put skip list ('Elapsed days calculator');
another = 'Y';
do while ((another = 'Y') | (another = 'y'));
put skip list ('First date as YYYY-MM-DD : ');
date1 = get_date();
put list ('Second date as YYYY-MM-DD : ');
date2 = get_date();
put skip edit ('Elapsed days = ', date2-date1) (a,f(6));
put skip list ('Do another (y/n)? ');
get edit (another) (a);
end;
 
/*
say Date.new('2029-03-29') - Date.new('2019-03-29');
* Read a date in YYYY-MM-DD format from the
* console and return its serial date equivalent
*/
get_date: proc returns (float bin);
dcl date char(20) varying;
dcl (y, m, d) float bin;
get edit (date) (a);
y = binary(substr(date,1,4));
m = binary(substr(date,6,2));
d = binary(substr(date,9,2));
return (serial_date(y,m,d));
end get_date;
 
/*
say Date.new('2019-01-01') + 90;
* Given a year, month and day in the Gregorian
* calendar, return a numeric date which is equal
* to the number of days since the start of the
* Common era
*/
serial_date: proc (yr, mo, da) returns (float bin);
dcl (yr, mo, da) float bin;
dcl n float bin;
n = 365 * yr + da + 31 * (mo - 1);
if (mo >= 3) then
n = n - fixed(0.4 * mo + 2.3);
else
yr = yr - 1;
n = n + fixed(yr/4) - fixed(0.75 * fixed(yr/100) + 1);
return (n);
end serial_date;
 
end elapsed_days;
say Date.new('2020-01-01') + 90;
</syntaxhighlight>
{{out}}
Test case taken from the Delphi example
<pre>
Elapsed Date Calculator
First date as YYYY-MM-DD : 1970-01-01
Second date as YYYY-MM-DD : 2019-10-18
Elapsed days = 18187
Do another (y/n)? n
</pre>
 
=={{header|PL/M}}==
say Date.new('2019-02-29') + 30;
{{Trans|FreeBASIC}}
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)<br>
Note that as the 8080 PL/M compiler only supports 8 and 16 bit unsigned integers, the dates must be at most 65535 days apart.
<syntaxhighlight lang="plm">
100H: /* CALCULATE THE NUMBER OF DAYS BETWEEN TWO DATES; BASED ON FREEBASIC */
 
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
CATCH { default { .message.say; exit; } };</lang>
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
PR$SIGNED: PROCEDURE( N ); /* PRINTS N AS A SIGNED INTEGER */
DECLARE N ADDRESS;
IF N <= 32767
THEN CALL PR$NUMBER( N );
ELSE DO;
CALL PR$CHAR( '-' );
CALL PR$NUMBER( - N );
END;
END PR$SIGNED ;
 
/* TASK */
<pre>272
 
28
/* RETURNS THE GREGORIAN DAY CORRESPONDING TO YYYY/MM/DD */
29
GREGORIAN: PROCEDURE( YYYY$MM$DD )ADDRESS;
3653
DECLARE YYYY$MM$DD ADDRESS;
2019-04-01
DECLARE DATE BASED YYYY$MM$DD ( 10 )BYTE;
2020-03-31
DECLARE ( YYYY, MM, DD, N, W ) ADDRESS;
Day out of range. Is: 29, should be in 1..28</pre>
 
DIGIT: PROCEDURE( D )BYTE; DECLARE D BYTE; RETURN D - '0'; END;
 
YYYY = ( DIGIT( DATE( 0 ) ) * 1000 ) + ( DIGIT( DATE( 1 ) ) * 100 )
+ ( DIGIT( DATE( 2 ) ) * 10 ) + DIGIT( DATE( 3 ) );
MM = ( DIGIT( DATE( 5 ) ) * 10 ) + DIGIT( DATE( 6 ) );
DD = ( DIGIT( DATE( 8 ) ) * 10 ) + DIGIT( DATE( 9 ) );
N = ( MM + 9 ) - ( ( ( MM + 9 ) / 12 ) * 12 );
W = YYYY - ( N / 10 );
RETURN ( 365 * W ) + ( W / 4 ) - ( W / 100 ) + ( W / 400 )
+ ( ( ( N * 306 ) + 5 ) / 10 ) + ( DD - 1 );
END GREGORIAN ;
 
/* SHOWS TTHE DAYS DIFFERENCE BETWEEN FROM$G AND TO$$G */
PR$DAYS$DIFFERENCE: PROCEDURE( FROM$DATE, TO$DATE );
DECLARE ( FROM$DATE, TO$DATE )ADDRESS;
CALL PR$STRING( .'DAYS BETWEEN $' );CALL PR$STRING( FROM$DATE );
CALL PR$STRING( .' AND $' );CALL PR$STRING( TO$DATE );
CALL PR$STRING( .' IS $' );
CALL PR$SIGNED( GREGORIAN( TO$DATE ) - GREGORIAN( FROM$DATE ) );
CALL PR$NL;
END PR$DAYS$DIFFERENCE ;
 
CALL PR$DAYS$DIFFERENCE( .'1902-01-01$', .'1968-12-25$' );
CALL PR$DAYS$DIFFERENCE( .'2019-01-01$', .'2019-01-02$' );
CALL PR$DAYS$DIFFERENCE( .'2019-01-02$', .'2019-01-01$' );
CALL PR$DAYS$DIFFERENCE( .'2019-01-01$', .'2019-03-01$' );
CALL PR$DAYS$DIFFERENCE( .'2020-01-01$', .'2020-03-01$' );
CALL PR$DAYS$DIFFERENCE( .'1995-11-21$', .'1995-11-21$' );
CALL PR$DAYS$DIFFERENCE( .'2090-01-01$', .'2098-12-25$' );
 
EOF
</syntaxhighlight>
{{out}}
<pre>
DAYS BETWEEN 1902-01-01 AND 1968-12-25 IS 24465
DAYS BETWEEN 2019-01-01 AND 2019-01-02 IS 1
DAYS BETWEEN 2019-01-02 AND 2019-01-01 IS -1
DAYS BETWEEN 2019-01-01 AND 2019-03-01 IS 59
DAYS BETWEEN 2020-01-01 AND 2020-03-01 IS 60
DAYS BETWEEN 1995-11-21 AND 1995-11-21 IS 0
DAYS BETWEEN 2090-01-01 AND 2098-12-25 IS 3280
</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
 
#!/usr/bin/python
Line 279 ⟶ 2,017:
two = sys.argv[2]
print diff(one,two)
</langsyntaxhighlight>
 
{{out}}
Line 285 ⟶ 2,023:
272
</pre>
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
'Task
'Calculate the number of days between two dates.
'Date input should be of the form YYYY-MM-DD.
 
from$ = "2000-05-29"
to$ = "2022-05-29"
Print NumberOfDays(from$, to$)
 
End
 
Function NumberOfDays (from$, to$)
NumberOfDays = 0
Dim As Integer Year(1 To 2), Mounth(1 To 2), Day(1 To 2)
Dim As Integer NumberD, Index
Year(1) = Val(Left$(from$, 4))
Year(2) = Val(Left$(to$, 4))
Mounth(1) = Val(Mid$(from$, 6, 2))
Mounth(2) = Val(Mid$(to$, 6, 2))
Day(1) = Val(Right$(from$, 2))
Day(2) = Val(Right$(to$, 2))
 
 
If Year(1) > Year(2) Then
Swap Year(1), Year(2)
Swap mount(1), mount(2)
Swap Day(1), Day(2)
End If
 
If Day(1) > Day(2) Then
Select Case Mounth(2) - 1
Case 4, 6, 9, 11
Day(2) = Day(2) + 30
Case 1, 3, 5, 7, 8, 10, 12
Day(2) = Day(2) + 31
Case 2
If (Index Mod 4 = 0) Or ((Index Mod 100 = 0) And (Index Mod 400 = 0)) Then Day(2) = Day(2) + 29 Else Day(2) = Day(2) + 28
End Select
Mounth(2) = Mounth(2) - 1
If Mounth(2) = 0 Then Year(2) = Year(2) - 1: Mounth(2) = 12
End If
 
NumberD = (Day(2) - Day(1)) + 1
 
For Index = Mounth(1) To Mounth(2) - 1 Step 1
Select Case Index
Case 4, 6, 9, 11
NumberD = NumberD + 30
Case 1, 3, 5, 7, 8, 10, 12
NumberD = NumberD + 31
Case 2
If (Index Mod 4 = 0) Or ((Index Mod 100 = 0) And (Index Mod 400 = 0)) Then NumberD = NumberD + 29 Else NumberD = NumberD + 28
End Select
 
Next
 
For Index = Year(1) To Year(2) - 1 Step 1
If (Index Mod 4 = 0) Or ((Index Mod 100 = 0) And (Index Mod 400 = 0)) Then NumberD = NumberD + 366 Else NumberD = NumberD + 365
Next Index
NumberOfDays = NumberD
End Function
 
 
</syntaxhighlight>
 
=={{header|Quackery}}==
 
The number of days between two dates is always positive – there is no such thing as -5 days. Strictly, it returns the number of midnights between two dates. As the task stands two consecutive dates are considered to have one day between them, which is a little weird but we know what it means.
 
<syntaxhighlight lang="Quackery"> [ 0 ] is january ( --> n )
[ 31 ] is february ( --> n )
[ 59 ] is march ( --> n )
[ 90 ] is april ( --> n )
[ 120 ] is may ( --> n )
[ 151 ] is june ( --> n )
[ 181 ] is july ( --> n )
[ 212 ] is august ( --> n )
[ 243 ] is september ( --> n )
[ 273 ] is october ( --> n )
[ 304 ] is november ( --> n )
[ 334 ] is december ( --> n )
 
[ 1 - dup 4 /
over 100 / -
swap 400 / + ] is leapcount ( y --> n )
 
[ dup leapcount
swap 365 * + ] is yeardays ( y --> n )
 
[ dup 400 mod 0 = iff
[ drop true ] done
dup 100 mod 0 = iff
[ drop false ] done
4 mod 0 = ] is leap ( y --> b )
 
[ do dup yeardays
dip
[ over february >
iff [ leap + ]
else drop ]
+ + ] is daycount ( [dmy] --> n )
 
[ daycount swap
daycount - abs ] is daysbetween ( [dmy] [dmy] --> n )
 
 
say "Days between 1995-11-21 and 1995-11-21 is "
' [ 21 november 1995 ] ' [ 21 november 1995 ] daysbetween echo cr
 
say "Days between 2019-01-01 and 2019-01-02 is "
' [ 1 january 2019 ] ' [ 2 january 2019 ] daysbetween echo cr
 
say "Days between 2019-01-02 and 2019-01-01 is "
' [ 2 january 2019 ] ' [ 1 january 2019 ] daysbetween echo cr
 
say "Days between 2019-01-01 and 2019-03-01 is "
' [ 1 january 2019 ] ' [ 1 march 2019 ] daysbetween echo cr
 
say "Days between 2020-01-01 and 2020-03-01 is "
' [ 1 january 2020 ] ' [ 1 march 2020 ] daysbetween echo cr
 
say "Days between 1902-01-01 and 1968-12-25 is "
' [ 1 january 1902 ] ' [ 25 december 1968 ] daysbetween echo cr
 
say "Days between 2090-01-01 and 2098-12-25 is "
' [ 1 january 2090 ] ' [ 25 december 2098 ] daysbetween echo cr
 
say "Days between 1902-01-01 and 2098-12-25 is "
' [ 1 january 1902 ] ' [ 25 december 2098 ] daysbetween echo cr</syntaxhighlight>
 
{{out}}
 
<pre>Days between 1995-11-21 and 1995-11-21 is 0
Days between 2019-01-01 and 2019-01-02 is 1
Days between 2019-01-02 and 2019-01-01 is 1
Days between 2019-01-01 and 2019-03-01 is 59
Days between 2020-01-01 and 2020-03-01 is 60
Days between 1902-01-01 and 1968-12-25 is 24465
Days between 2090-01-01 and 2098-12-25 is 3280
Days between 1902-01-01 and 2098-12-25 is 71947
</pre>
 
 
=={{header|Raku}}==
(formerly Perl 6)
Dates are first class objects in Raku and may have arithmetic in days done directly on them.
<syntaxhighlight lang="raku" line>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; } };</syntaxhighlight>
 
<pre>272
28
29
3653
2019-04-01
2020-03-31
Day out of range. Is: 29, should be in 1..28</pre>
 
=={{header|REBOL}}==
REBOL accepts a multitude of lexically recognized date! formats. date! is a builtin datatype. Slashes or dashes, year-month-day, day/month/year, or dd/monthname/year, etc. Math on dates defaults to days.
 
<pre>prompt$ rebol -q
>> 2019-11-24 - 2000-01-01
== 7267
>> 2019-nov-24 - 01-jan-2000
== 7267</pre>
 
=={{header|REXX}}==
Line 292 ⟶ 2,210:
<br>days since the beginning of the Gregorian calendar, &nbsp; and &nbsp; '''I''' &nbsp; which is the option that indicates the date is in
<br>the &nbsp; '''ISO''' &nbsp; (International Standards Organization standard 8601:2004) &nbsp; format.
<langsyntaxhighlight lang="rexx">/*REXX program computes the number of days between two dates in the form of YYYY-MM-DD */
parse arg $1 $2 . /*get 2 arguments (dates) from the C.L.*/
say abs( date('B',$1,"I") - date('B',$2,"I") ) ' days between ' $1 " and " $2
/*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> 2019-10-02 &nbsp; 2000-01-01 </tt>}}
<pre>
68487214 days between 2019-10-02 and 2000-01-01
</pre>
 
Line 305 ⟶ 2,223:
<br>allowing a single asterisk &nbsp; ('''*''') &nbsp; to be used for a date &nbsp; (which signifies that the current date is to be used).
 
The dates may be specified in the following formats:
Commas &nbsp; (''',''') &nbsp; are inserted into numbers where appropriate.
, (a comma) indicates today's date
* (an asterisk) indicates today's date
yyyy-mm-dd where <big><b>yyyy</b></big> may be a 2- or 4-digit year, <big><b>mm</b></big> may be a 1- or 2-digit month, <big><b>dd</b></big> may be a 1- or 2-digit day of month
mm/dd/yyyy (as above)
mm/dd (as above), but the current year is assumed
dd\mm\yyyy (as above)
dd\mm (as above), but the current year is assumed
<br><br>Commas &nbsp; (''',''') &nbsp; are inserted into numbers where appropriate.
 
Also, more informative error messages are generated.
<langsyntaxhighlight lang="rexx">/*REXX program computes the number of days between two dates in the form of YYYY-MM-DD */
parse arg $.1 $.2 _ . 1 . . xtra /*obtain two arguments from the C.L. */
ifseps= $.1=='*/-\'; then $.1= date("I") yr.= .; mon.= .; dd.= . /*obtaindefine currentthe datedefaults iffor it'sboth an *dates. */
if $.2=='*' then $.2= date("I") do a=1 for 2 /* " " " "/*process both of " " " the specified dates. */
parse var $.1 yr.1 '-' mon.1 "-" dd.1 if $.a=='' | /$.a=="*obtain" the| constituents$.a==',' of 1stthen date$.a= */date("I")
parse var $.2 yr.2 '-' mon.2 "-" dd.2 do s=1 for /*length(seps) " " /*process a specified date " " 2nd " by separator*/
sep= substr(seps, s, 1) /*obtain one of the supported sep char.*/
if pos(sep, $.a)\==0 then call conv $.a /*parse the date string.*/
end /*s*/
end /*a*/
?.1= '1st'
?.2= '2nd'
if _ \== '' then call err "too many arguments specified: " xtra
dy.=31 31 /*default number of days for all months*/
parse value 30 with dy.4 1 dy.6 1 dy.9 1 dy.11 /*define 30─day months, Feb. is special*/
@notCorr= "isn't in thea correctsupport date format: " YYYY-MM-DD " /*used for a literal for an error msg. */
 
do j=1 for 2 /*examine both dates for correct format*/
Line 342 ⟶ 2,272:
end /*j*/
 
saybetween= commas(abs( date('B', $.1, "I") - date('B', $.2, "I")) ) ' days between '/* # $.1days between " and " $dates.2*/
exitparse source . how . /*stick a fork in/*determine it,how we're all doneinvoked. */
if how='COMMAND' then say commas(between) ' days between ' $.1 " and " $.2
exit between /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do c_=length(_)-3 to 1 by -3; _=insert(',', _, c_); end; return _
err: say; say '***error*** ' arg(1); exit 13 /*issue an error message (with text) */
isDec: return verify( arg(1), 1234567890) \== 0 /*insure argument is just decimal digs.*/
leapYr: arg _; ly=_//4==0; if ly==0 then return 0; ly=((_//100\==0)|_//400==0); return ly</lang>
serDAT: call err 'illegal date format with a separator= ['sep"], date= " @dat
/*──────────────────────────────────────────────────────────────────────────────────────*/
conv: parse arg @dat /*obtain date that the user specified. */
if sep=='-' then parse var @dat yr.a "-" mon.a '-' dd.a /* yy-mm-dd */
if sep=='/' then parse var @dat mon.a "/" dd.a '/' yr.a /* mm/dd/yy */
if sep=='\' then parse var @dat dd.a "\" mon.a '\' yr.a /* dd\mm\yy */
if yr.a=='' then yr.a= right( date(), 4) /*omitted yy*/
if length(yr.a)==2 then yr.a= left( date('S'), 2)yr.a /*2 dig yy ?*/
if yr.a==. | mon.a==. | dd.a==. then call serDAT /*validate. */
return</syntaxhighlight>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> * &nbsp; 2000-1-1 </tt>}}
 
Today &nbsp; (indicated by the asterisk) &nbsp; is &nbsp; 2019-10-2
<pre>
67,848214 days between 2019-10-02 and 2000-01-01
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "stdlib.ring"
 
DaysBetween = [["1995-11-21","1995-11-21"],
["2019-01-01","2019-01-02"],
["2019-01-02","2019-01-01"],
["2019-01-01","2019-03-01"],
["2020-01-01","2020-03-01"],
["1902-01-01","1968-12-25"],
["2090-01-01","2098-12-25"]]
 
for n = 1 to len(DaysBetween)
date1 = DaysBetween[n][1]
date2 = DaysBetween[n][2]
date3 = substr(date1,9,2) + "/" + substr(date1,6,2) + "/" + substr(date1,1,4)
date4 = substr(date2,9,2) + "/" + substr(date2,6,2) + "/" + substr(date2,1,4)
? "Days between " + DaysBetween[n][1] + " and " + DaysBetween[n][2] + ": " + diffdays(date4,date3)
next
</syntaxhighlight>
{{out}}
<pre>
Days between 1995-11-21 and 1995-11-21: 0
Days between 2019-01-01 and 2019-01-02: 1
Days between 2019-01-02 and 2019-01-01: -1
Days between 2019-01-01 and 2019-03-01: 59
Days between 2020-01-01 and 2020-03-01: 60
Days between 1902-01-01 and 1968-12-25: 0
Days between 2090-01-01 and 2098-12-25: 3280
</pre>
 
=={{header|RPL}}==
Uses Python formula, in a forced binary calculation mode to avoid 'flooring' instructions
{{works with|Halcyon Calc|4.2.7}}
≪ → d m y
≪ m 9 + 12 MOD
y OVER #10d / -
DUP 365 * OVER #4d / + OVER #100d / - SWAP #400d / +
SWAP 306 * 5 + #10d / + d + 1 - B→R
≫ ≫
'GREGN' STO
≪ SWAP 1 2 '''START'''
→ date
≪ date 9 10 SUB STR→ date 6 7 SUB STR→ date 1 4 SUB STR→
GREGN SWAP
'''NEXT''' -
'NBDAYS' STO
 
"1902-01-01" "1968-12-25" NBDAYS
"2019-01-02" "2019-01-01" NBDAYS
"2019-01-01" "2019-03-01" NBDAYS
"2020-01-01" "2020-03-01" NBDAYS
{{out}}
<pre>
4: 24465
3: -1
2: 59
1: 60
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "date"
 
d1, d2 = Date.parse("2019-1-1"), Date.parse("2019-10-19")
Line 363 ⟶ 2,368:
p (d1 - d2).to_i # => -291
p (d2 - d1).to_i # => 291
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
// chrono = "0.4"
 
use chrono::NaiveDate;
 
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() != 3 {
eprintln!("usage: {} start-date end-date", args[0]);
std::process::exit(1);
}
if let Ok(start_date) = NaiveDate::parse_from_str(&args[1], "%F") {
if let Ok(end_date) = NaiveDate::parse_from_str(&args[2], "%F") {
let d = end_date.signed_duration_since(start_date);
println!("{}", d.num_days());
} else {
eprintln!("Can't parse end date");
std::process::exit(1);
}
} else {
eprintln!("Can't parse start date");
std::process::exit(1);
}
}</syntaxhighlight>
 
{{out}}
<pre>
days_between_dates 2020-01-01 2020-09-06
249
</pre>
 
=={{header|S-BASIC}}==
Error checking of entered dates is omitted in order to focus
on the stated task but would obviously have to be included
in production code.
<syntaxhighlight lang = "BASIC">
comment
Given a month, day, and year in the Gregorian calendar,
return a numeric date which is equal to the number of
days since the start of the Common era.
end
function serial_date(da, mo, yr = integer) = real
var n = real
n = 365 * yr + da + 31 * (mo - 1)
if mo >= 3 then
n = n - int(.4 * mo + 2.3)
else
yr = yr - 1
n = n + int(yr/4) - int(.75 * (int(yr/100) + 1))
end = n
 
comment
Read a date in YYYY-MM-DD format from the console and
return its serial date equivalent.
end
function get_date = real
var date = string : 20
var y, m, d = integer
input2 date
y = val(mid(date,1,4))
m = val(mid(date,6,2))
d = val(mid(date,9,2))
end = serial_date(d, m, y)
 
rem -- main program begins here
 
var date1, date2 = real
var another = char
 
repeat
begin
print "First date : ";
date1 = get_date
print "Second date : ";
date2 = get_date
print "Elapsed days = "; date2 - date1
input "Do another (y/n)"; another
end
until not another
 
end
</syntaxhighlight>
{{out}}
Test dates taken from Delphi example
<pre>
First date : 1970-01-01
Second date : 2019-10-18
Elapsed days = 18187
Do another (y/n)? n
</pre>
 
=={{header|Scala}}==
<syntaxhighlight lang="scala">object DaysBetweenDates {
 
/*Inspired by the Python version of the algorithm and the discussion here
https://stackoverflow.com/questions/12862226/the-implementation-of-calculating-the-number-of-days-between-2-dates.*/
 
/**Transform a date into a day number in the Gregorian Calendar*/
def dateToDays(year : Int, month : Int, day : Int ) : Int = {
val m = (month+ 9) % 12
val y = year - m/10
val d = day
365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (d - 1)
}
 
/**Compute the difference of days between both input dates*/
def daysDifference(firstDate : String, secondDate : String) : Int = {
val firstDateTuple = firstDate.split('-') match { case Array(a, b, c) => (a, b, c) }
val secondDateTuple = secondDate.split('-') match { case Array(a, b, c) => (a, b, c) }
 
val firstYear = dateToDays( firstDateTuple._1.toInt, firstDateTuple._2.toInt, firstDateTuple._3.toInt)
val secondYear = dateToDays( secondDateTuple._1.toInt, secondDateTuple._2.toInt, secondDateTuple._3.toInt )
 
return secondYear - firstYear
}
 
def main(args: Array[String]): Unit = {
println(daysDifference("2019-01-01", "2019-09-30"))
println(daysDifference("1995-11-21", "1995-11-21"))
println(daysDifference("2019-01-01", "2019-01-02"))
println(daysDifference("2019-01-02", "2019-01-01"))
println(daysDifference("2019-01-01", "2019-03-01"))
println(daysDifference("2020-01-01", "2020-03-01"))
println(daysDifference("1902-01-01", "1968-12-25"))
println(daysDifference("2090-01-01", "2098-12-25"))
println(daysDifference("1902-01-01", "2098-12-25"))
}
 
}
</syntaxhighlight>
{{out}}
<pre>
272
0
1
-1
59
60
24465
3280
71947
</pre>
 
=={{header|SenseTalk}}==
SenseTalk supports date and time calculations in many forms, and many different units of time (such as 'days' in this example). Rounding is needed due to daylight savings time changes.
<syntaxhighlight lang="sensetalk">set startDate to "2020-03-13"
set endDate to "2021-07-14"
 
put (endDate - startDate) rounded to nearest day
</syntaxhighlight>
{{out}}
<pre>
488 days
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">require('Date::Calc')
 
func days_diff(a,b) {
%S<Date::Calc>.Delta_Days(a.split('-')..., b.split('-')...)
}
 
var date1 = "1970-01-01"
var date2 = "2019-10-02"
 
say "Date 1: #{date1}"
say "Date 2: #{date2}"
 
var days = days_diff(date1, date2)
 
say "There are #{days} days between these dates"</syntaxhighlight>
{{out}}
<pre>
Date 1: 1970-01-01
Date 2: 2019-10-02
There are 18171 days between these dates
</pre>
 
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">import Foundation
 
func daysFromTimeInterval(_ interval: Double) -> Int {
return Int(interval) / 86400
}
 
let formatter = DateFormatter()
 
formatter.dateFormat = "yyyy-MM-dd"
 
print("Enter date one (yyyy-MM-dd): ", terminator: "")
 
guard let date1Str = readLine(strippingNewline: true), let date1 = formatter.date(from: date1Str) else {
fatalError("Invalid date two")
}
 
print("Enter date two (yyyy-MM-dd): ", terminator: "")
 
guard let date2Str = readLine(strippingNewline: true), let date2 = formatter.date(from: date2Str) else {
fatalError("Invalid date two")
}
 
let (start, end) = date1 > date2 ? (date2, date1) : (date1, date2)
let days = daysFromTimeInterval(DateInterval(start: start, end: end).duration)
 
print("There are \(days) days between \(start) and \(end)")</syntaxhighlight>
 
{{out}}
 
<pre>Enter date one (yyyy-MM-dd): 2019-01-01
Enter date two (yyyy-MM-dd): 2019-12-02
There are 335 days between 2019-01-01 05:00:00 +0000 and 2019-12-02 05:00:00 +0000</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again Shell}}
 
<syntaxhighlight lang="bash"># return true if year is leap in Gregorian calendar
leap() {
local -i year
year=$1
if (( year % 4 )); then return 1; fi
if (( year % 100 )); then return 0; fi
! (( year % 400 ))
}
 
# convert date to Gregorian day count (Rata Die), where RD 1 = January 1, 1 CE
rd() {
local year month day
IFS=- read year month day <<<"$1"
local -i elapsed_years=year-1
(( day += elapsed_years * 365 ))
(( day += elapsed_years/4 ))
(( day -= elapsed_years/100 ))
(( day += elapsed_years/400 ))
local month_lengths=(31 28 31 30 31 30 31 31 30 31 30 31)
if leap "$year"; then let month_lengths[1]+=1; fi
local m
for (( m=0; m<month-1; ++m)); do
(( day += month_lengths[m] ))
done
printf '%d\n' "$day"
}
 
days_between() {
local -i date1 date2
date1=$(rd "$1")
date2=$(rd "$2")
printf '%d\n' $(( date2 - date1 ))
}
 
days_between 1970-01-01 2019-12-04
</syntaxhighlight>
 
{{Out}}
<pre>18234</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Globalization
 
Module Module1
 
Function DateDiff(d1 As String, d2 As String) As Integer
Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture)
Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture)
Return (b - a).TotalDays
End Function
 
Sub Main()
Console.WriteLine(DateDiff("1970-01-01", "2019-10-18"))
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>18187</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import time
 
fn days_between(d1 string, d2 string) ?int {
t1 := time.parse('$d1 01:01:01')?
t2 := time.parse('$d2 01:01:01')?
days := int((t2-t1).hours()/24)
return days
}
fn main(){
mut date1,mut date2 := "2019-01-01", "2019-09-30"
mut days := days_between(date1, date2)?
println("There are $days days between $date1 and $date2")
date1, date2 = "2015-12-31", "2016-09-30"
days = days_between(date1, date2)?
println("There are $days days between $date1 and $date2")
}</syntaxhighlight>
{{out}}
<pre>
There are 272 days between 2019-01-01 and 2019-09-30
There are 274 days between 2015-12-31 and 2016-09-30</pre>
 
=={{header|Wren}}==
{{libheader|Wren-date}}
<syntaxhighlight lang="wren">import "./date" for Date
 
var datePairs = [
["1995-11-21", "1995-11-21"],
["2019-01-01", "2019-01-02"],
["2019-01-02", "2019-01-01"],
["2019-01-01", "2019-03-01"],
["2020-01-01", "2020-03-01"],
["1902-01-01", "1968-12-25"],
["2090-01-01", "2098-12-25"],
["1902-01-01", "2098-12-25"],
["1970-01-01", "2019-10-18"],
["2019-03-29", "2029-03-29"],
["2020-02-29", "2020-03-01"]
]
Date.default = Date.isoDate
for (dates in datePairs) {
var date1 = Date.parse(dates[0])
var date2 = Date.parse(dates[1])
var days = (date2 - date1).days
System.print("Days between %(date1) and %(date2) = %(days)")
}</syntaxhighlight>
 
{{out}}
<pre>
Days between 1995-11-21 and 1995-11-21 = 0
Days between 2019-01-01 and 2019-01-02 = 1
Days between 2019-01-02 and 2019-01-01 = -1
Days between 2019-01-01 and 2019-03-01 = 59
Days between 2020-01-01 and 2020-03-01 = 60
Days between 1902-01-01 and 1968-12-25 = 24465
Days between 2090-01-01 and 2098-12-25 = 3280
Days between 1902-01-01 and 2098-12-25 = 71947
Days between 1970-01-01 and 2019-10-18 = 18187
Days between 2019-03-29 and 2029-03-29 = 3653
Days between 2020-02-29 and 2020-03-01 = 1
</pre>
 
=={{header|XPL0}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang "XPL0">func Gregorian(Y, M, D); \Return Gregorian day given date
int Y, M, D;
int N, W;
[N:= M + 9 - (M+9)/12*12;
W:= Y - N/10;
return 365*W + W/4 - W/100 + W/400 + (N*306+5)/10 + D - 1;
];
 
int Dates, N, Y, M, D, G0, G1;
[Dates:= [
["2019-01-01", "2019-01-02"],
["2019-01-02", "2019-01-01"],
["2019-01-01", "2019-03-01"],
["2020-01-01", "2020-03-01"],
["1995-11-21", "1995-11-21"],
["2090-01-01", "2098-12-25"] ];
OpenO(8); OpenI(8);
for N:= 0 to 6-1 do
[Text(8, Dates(N,0));
Y:= IntIn(8); M:= IntIn(8); D:= IntIn(8);
G0:= Gregorian(Y, M, D);
Text(8, Dates(N,1));
Y:= IntIn(8); M:= IntIn(8); D:= IntIn(8);
G1:= Gregorian(Y, M, D);
Text(0, "Number of days between "); Text(0, Dates(N,0)); Text(0, " and ");
Text(0, Dates(N,1)); Text(0, " is "); IntOut(0, abs(G1-G0)); CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
Number of days between 2019-01-01 and 2019-01-02 is 1
Number of days between 2019-01-02 and 2019-01-01 is 1
Number of days between 2019-01-01 and 2019-03-01 is 59
Number of days between 2020-01-01 and 2020-03-01 is 60
Number of days between 1995-11-21 and 1995-11-21 is 0
Number of days between 2090-01-01 and 2098-12-25 is 3280
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">var [const] TD=Time.Date;
today:=TD.parseDate("--"); // "yyyy-mm-dd" and variations --> (y,m,d)
// or Time.Clock.UTC --> (y,m,d,h,m,s)
Line 375 ⟶ 2,761:
TD.toYMDString(today.xplode()), // to(y,m,d) not to((y,m,d))
TD.toYMDString(then.xplode()),
TD.deltaDays(today,then.xplode())));</langsyntaxhighlight>
{{out}}
<pre>
1,462

edits