Jump to content

Days between dates: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 179:
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}}==
Based on...
{{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|AppleScript}}==
3,043

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.