Days between dates: Difference between revisions

no edit summary
(Added S-BASIC example)
No edit summary
(2 intermediate revisions by 2 users not shown)
Line 1,248:
Days between 2090-01-01 and 2098-12-25 is 3280 days
</pre>
 
 
=={{header|Frink}}==
Line 1,725 ⟶ 1,724:
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}}==
Line 1,792 ⟶ 1,833:
272
274
</pre>
 
=={{header|PL/I-80}}==
{{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;
 
/*
* 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;
 
/*
* 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;
</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>
 
45

edits