Days between dates: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 1,606: | Line 1,606: | ||
272 |
272 |
||
</pre> |
</pre> |
||
=={{header|QB64}}== |
|||
<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 NumberY, NumberM, 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 |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |