Author 
Topic: Time Calculator (Read 1416 times) 

rcdronan
Guest


Time Calculator
« Thread started on: Jul 8^{th}, 2016, 6:42pm » 

Here is the time calculator I made a while back:
NOTE: See the updated version at bottom of post.
This one gets the difference between two dates: Code:'get user input
input "Please type the first date in MM/DD/YYYY format: "; firstDate$
input "Please type the second date in MM/DD/YYYY format: "; secondDate$
'prosess input
firstMonth = val(mid$(firstDate$, 1, 2))
firstDay = val(mid$(firstDate$, 4, 2))
firstYear = val(mid$(firstDate$, 7))
secondMonth = val(mid$(secondDate$, 1, 2))
secondDay = val(mid$(secondDate$, 4, 2))
secondYear = val(mid$(secondDate$, 7))
'set defaults
'gosub [setVariables]
'Start of program
1 currantMonth = firstMonth
currantDay = firstDay
currantYear = firstYear
gosub [setVariables]
if firstMonth = secondMonth and firstYear = secondYear then
total = secondDay  firstDay
goto [finished]
end if
total = daysInMonth  currantDay
currantMonth = currantMonth + 1
gosub [setVariables]
2 if currantYear = secondYear then
'count each month as it goes by to month before second date
for currantMonth = currantMonth to secondMonth  1
gosub [setVariables]
total = total + daysInMonth
next currantMonth
currantMonth = currantMonth + 1
gosub [setVariables]
'count to the days in the last date
total = total + secondDay
goto [finished]
end if
for currantMonth = currantMonth to 12
gosub [setVariables]
total = total + daysInMonth
next currantMonth
''
currantMonth = 1 '
currantDay = 1 ' <new!!!
currantYear = currantYear + 1 '
''
3 gosub [setVariables]
if firstYear < secondYear + 1 then
for currantYear = currantYear to secondYear  1
gosub [setVariables]
total = total + daysInYear
next currantYear
end if
4 for currantMonth = 1 to secondMonth  1
gosub [setVariables]
total = total + daysInMonth
next currantMonth
5 total = total + secondDay
6
[finished]
print "The number of days difference between "; firstDate$; " and "; secondDate$; " is "; total; " days."
'wait for key press
input ""; dummyVar
'end the program
end
'
[setVariables]
'check for bug
if (currantMonth > 12) then curantMonth = currantMonth  12:currantYear = currantYear + 1:currantDay = 1
'calculate leapYear
if (currantYear / 4 <> int(currantYear / 4)) then leapYear = 0
if (currantYear / 4 = int(currantYear / 4)) then leapYear = 1
'calculate daysInYear
if (leapYear = 0) then daysInYear = 365
if (leapYear = 1) then daysInYear = 366
'calculate daysInMonth
if (currantMonth = 1) then daysInMonth = 31
if (currantMonth = 2) and (leapYear = 0) then daysInMonth = 28
if (currantMonth = 2) and (leapYear = 1) then daysInMonth = 29
if (currantMonth = 3) then daysInMonth = 31
if (currantMonth = 4) then daysInMonth = 30
if (currantMonth = 5) then daysInMonth = 31
if (currantMonth = 6) then daysInMonth = 30
if (currantMonth = 7) then daysInMonth = 31
if (currantMonth = 8) then daysInMonth = 31
if (currantMonth = 9) then daysInMonth = 30
if (currantMonth = 10) then daysInMonth = 31
if (currantMonth = 11) then daysInMonth = 30
if (currantMonth = 12) then daysInMonth = 31
return
This one gets the difference between two dates with the times of day: Code:'Time Calculator:
'IT WORKS!!!
'Get user inputs:
input "Please type the first time in SS:MM:HH 24 hour format: "; firstTime$
input "Please type the first date in DD/MM/YYYY format: "; firstDate$
input "Please type the second time in SS:MM:HH 24 hour format: "; secondTime$
input "Please type the second date in DD/MM/YYYY format: "; secondDate$
'Sort the input into variables:
firstSecond = val(mid$(firstTime$, 1, 2))
firstMinute = val(mid$(firstTime$, 4, 2))
firstHour = val(mid$(firstTime$, 7))
firstDay = val(mid$(firstDate$, 1, 2))
firstMonth = val(mid$(firstDate$, 4, 2))
firstYear = val(mid$(firstDate$, 7))
secondSecond = val(mid$(secondTime$, 1, 2))
secondMinute = val(mid$(secondTime$, 4, 2))
secondHour = val(mid$(secondTime$, 7))
secondDay = val(mid$(secondDate$, 1, 2))
secondMonth = val(mid$(secondDate$, 4, 2))
secondYear = val(mid$(secondDate$, 7))
'Set up the currant variables
currantSecond = firstSecond
currantMinute = firstMinute
currantHour = firstHour
currantDay = firstDay
currantMonth = firstMonth
currantYear = firstYear
'Set variable defaults:
difference = 0
'Seconds:
if currantMinute = secondMinute and currantHour = secondHour and currantDay = secondDay and currantMonth = secondMonth and currantYear = secondYear then
difference = secondSecond  currantSecond
goto [end]
end if
'Work out how many seconds there are to the next minute:
difference = difference + (60  currantSecond)
currantMinute = currantMinute + 1
if currantMinute = 60 then currantMinute = 0:currantHour = currantHour + 1
'Minutes:
if currantHour = secondHour and currantDay = secondDay and currantMonth = secondMonth and currantYear = secondYear then
difference = difference + ((secondMinute  currantMinute) * 60)
currantHour = currantHour + 1
if currantHour = 24 then currantHour = 0:currantDay = currantDay + 1
goto [nextSecond]
end if
'Work out how many minutes there are to the next hour and times it by how many seconds there are in a minute:
difference = difference + ((60  currantMinute) * 60)
currantHour = currantHour + 1
if currantHour = 24 then currantHour = 0:currantDay = currantDay + 1
'Hours:
if currantDay = secondDay and currantMonth = secondMonth and currantYear = secondYear then
difference = difference + ((secondHour  currantHour) * 60 * 60)
currantDay = currantDay + 1
gosub [setDaysInMonthAndYear]
if currantDay > daysInMonth then currantDay = currantDay  daysInMonth:currantMonth = currantMonth + 1
goto [nextMinute]
end if
'Work out how many hours there are to the next day and times it by how many seconds there are in an hour:
difference = difference + ((24  currantHour) * 60 * 60)
currantDay = currantDay + 1
gosub [setDaysInMonthAndYear]
if currantDay > daysInMonth then currantDay = currantDay  daysInMonth:currantMonth = currantMonth + 1
'Days:
if currantMonth = secondMonth and currantYear = secondYear then
difference = difference + ((secondDay  currantDay) * 60 * 60 * 24)
currantMonth = currantMonth + 1
gosub [setDaysInMonthAndYear]
if currantMonth > 12 then currantMonth = currantMonth  12:currantYear = currantYear + 1
goto [nextHour]
end if
'Work out how many days there are to the next month and times it by how many seconds there are in a day:
difference = difference + ((daysInMonth  currantDay) * 60 * 60 * 24)
currantMonth = currantMonth + 1
gosub [setDaysInMonthAndYear]
if currantMonth > 12 then currantMonth = currantMonth  12:currantYear = currantYear + 1
'Months:
if currantYear = secondYear then
for currantMonth = currantMonth to secondMonth
gosub [setDaysInMonthAndYear]
difference = difference + (daysInMonth * 60 * 60 * 24)
next currantMonth
goto [nextDay]
end if
'Work out how many months there are to the next year and times it by how many seconds there are in the individual months:
for currantMonth = currantMonth to 12
gosub [setDaysInMonthAndYear]
difference = difference + (daysInMonth * 60 * 60 * 24)
next currantMonth
currantMonth = 1
currantYear = currantYear + 1
'Years:
'Work out how many years there are to the year before the second year and times it by how many seconds there are in the individual years:
gosub [setDaysInMonthAndYear]
for currantYear = currantYear to secondYear  1
gosub [setDaysInMonthAndYear]
difference = difference + (daysInYear * 60 * 60 * 24)
next currantYear
currantYear = currantYear + 1
[nextMonth]
'Months:
'Work out how many months there are to the month before the second month and times it by how many seconds there are in the individual months:
for currantMonth = 1 to secondMonth  1
gosub [setDaysInMonthAndYear]
difference = difference + (daysInMonth * 60 * 60 * 24)
next currantMonth
currantMonth = currantMonth + 1
[nextDay]
'Days:
'Work out how many days there are to the day before the second day and times it by how many seconds there are in a day:
difference = difference + (secondDay * 60 * 60 * 24)
currantDay = currantDay + 1
[nextHour]
'Hours:
'Work out how many hours there are to the hour before the second hour and times it by how many seconds there are in an hour:
difference = difference + (secondHour * 60 * 60)
currantHour = currantHour + 1
[nextMinute]
'Minutes:
'Work out how many minutes there are to the minute before the second minute and times it by how many seconds there are in a minute:
difference = difference + (secondMinute * 60)
currantMinute = currantMinute + 1
[nextSecond]
'Seconds:
'Work out how many seconds there are to the second second:
difference = difference + (secondSecond)
[end]
'Display output:
print "The difference between "; firstTime$; " "; firstDate$; " and "; secondTime$; " "; secondDate$; " is "; difference; " seconds."
'Wait
input ""; dummyVar$
end
[setDaysInMonthAndYear]
'calculate leapYear
if (currantYear / 4 <> int(currantYear / 4)) then leapYear = 0
if (currantYear / 4 = int(currantYear / 4)) then leapYear = 1
'calculate daysInYear
if (leapYear = 0) then daysInYear = 365
if (leapYear = 1) then daysInYear = 366
'calculate daysInMonth
if (currantMonth = 1) then daysInMonth = 31
if (currantMonth = 2) and (leapYear = 0) then daysInMonth = 28
if (currantMonth = 2) and (leapYear = 1) then daysInMonth = 29
if (currantMonth = 3) then daysInMonth = 31
if (currantMonth = 4) then daysInMonth = 30
if (currantMonth = 5) then daysInMonth = 31
if (currantMonth = 6) then daysInMonth = 30
if (currantMonth = 7) then daysInMonth = 31
if (currantMonth = 8) then daysInMonth = 31
if (currantMonth = 9) then daysInMonth = 30
if (currantMonth = 10) then daysInMonth = 31
if (currantMonth = 11) then daysInMonth = 30
if (currantMonth = 12) then daysInMonth = 31
return

« Last Edit: Aug 8^{th}, 2016, 02:01am by rcdronan » 
Logged




tsh73
JBSupporter
member is offline
Gender:
Posts: 3648


Re: Time Calculator
« Reply #1 on: Jul 9^{th}, 2016, 1:24pm » 

Hello rcdronan JB date commands support translation from/to number. (see Help file, "Date and Time Functions") So difference between dates could be easily done by subtracting: Code:print date$() 'Jul 09, 2016
print date$("07/09/2016") 'mm dd yyyy
print date$("06/30/2016") 'mm dd yyyy
print date$("07/09/2016")  date$("06/30/2016")
So day of months, leap year  all this could be left to JB. EDIT BUILDING BLOCKS::date EDIT2 also it is possible to store datetime in single numeric variable, having integer part for days and time in fractional part. Discussed in this thread Need Help With Program

« Last Edit: Jul 9^{th}, 2016, 1:34pm by tsh73 » 
Logged

Q: "And if I took your codes and compile them, and sell them for a profit"? A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it  please do. (enjoying JB 1.01 on WinXP, netbook and desktop)



rcdronan
Guest


Re: Time Calculator
« Reply #2 on: Jul 10^{th}, 2016, 9:52pm » 

Really? Well, I still like to know what goes on in the background in programs and I thought this would be a good exercise to see if I could do it. And besides, if you wanted to work out the difference between two dates before 1901 I don't think you would be able to do it this way.


Logged




ezprogramming
Guest


Re: Time Calculator
« Reply #3 on: Jul 11^{th}, 2016, 03:23am » 

Post deleted

« Last Edit: Sep 15^{th}, 2016, 1:44pm by ezprogramming » 
Logged




tsh73
JBSupporter
member is offline
Gender:
Posts: 3648


Re: Time Calculator
« Reply #4 on: Jul 11^{th}, 2016, 05:34am » 

rcdronan, reinventing the wheel is a honorable occupation But at least you should fix "leap year" calculation: Quote:In the Gregorian calendar, years that are divisible by 100, but not by 400, do not contain a leap day. Thus, 1700, 1800, and 1900 did not contain a leap day, 2100, 2200, and 2300 will not contain a leap day, while 1600 and 2000 did, and 2400 will. 



Logged

Q: "And if I took your codes and compile them, and sell them for a profit"? A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it  please do. (enjoying JB 1.01 on WinXP, netbook and desktop)



CarlGundel
Administrator
member is offline
Posts: 158


Re: Time Calculator
« Reply #5 on: Jul 11^{th}, 2016, 07:01am » 

If you discover that the negative numbers before 1901 doesn't work reliably for some case just let us know and we will deal with it as a bug report.
Carl


Logged




CarlGundel
Administrator
member is offline
Posts: 158


Re: Time Calculator
« Reply #6 on: Jul 11^{th}, 2016, 07:03am » 

on Jul 11^{th}, 2016, 05:34am, tsh73 wrote: Reinventing the wheel is a great way to learn, and we shouldn't discourage anyone from creating new date functions that aren't already available. The more the merrier.
Carl


Logged




rcdronan
Guest


Re: Time Calculator
« Reply #7 on: Jul 14^{th}, 2016, 6:40pm » 

Thanks! To solve the problem with the leap year calculation would you need to change it to something like this? Code:if (currantYear / 4 = int(currantYear / 4)) and ((currantYear / 100 <> int(currantYear / 100)) and (currantYear / 400 = int(currantYear / 400))) then leapYear = 1


Logged




tsh73
JBSupporter
member is offline
Gender:
Posts: 3648


Re: Time Calculator
« Reply #8 on: Jul 15^{th}, 2016, 01:38am » 

Quote:...to something like this? 

Yes to something like this. But you have to make it work  as of now, it doesn't.
Code:'In the Gregorian calendar, years that are divisible by 100, but not by 400, do not contain a leap day.
'Thus, 1700, 1800, and 1900 did not contain a leap day, 2100, 2200, and 2300 will not contain a leap day,
'while 1600 and 2000 did, and 2400 will.
data 1700, 1800, 1900, 2100, 2200, 2300, 1600, 2000, 2400, 0
read currantYear
print "year", "date$", "calculated", "calculated2"
while currantYear>0
read currantYear
print currantYear,
print isLeapYear(currantYear),
leapYear = 0
if (currantYear / 4 = int(currantYear / 4)) and ((currantYear / 100 <> int(currantYear / 100)) and (currantYear / 400 = int(currantYear / 400))) then leapYear = 1
print "leapYear "; leapYear;
'first, "currantYear / 4 = int(currantYear / 4)" in BASIC is (currantYear MOD 4 = 0)
'second, year could not be dividable by 400 and not by 100
'somehow I struggle to wrap it all in single IF
'so in two lines:
leapYear = (currantYear MOD 4 = 0)
if (currantYear MOD 100 = 0) and (currantYear MOD 400 <> 0) then leapYear = 0
print "", leapYear
wend
end
'
function isLeapYear(year)
isLeapYear = date$("02.29.";year)<>0
end function
EDIT here is singleline solution Code:leapYear = (currantYear MOD 4 = 0) and not((currantYear MOD 100 = 0) and (currantYear MOD 400 <> 0) )

« Last Edit: Jul 15^{th}, 2016, 01:45am by tsh73 » 
Logged

Q: "And if I took your codes and compile them, and sell them for a profit"? A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it  please do. (enjoying JB 1.01 on WinXP, netbook and desktop)



ezprogramming
Guest


Re: Time Calculator
« Reply #9 on: Jul 15^{th}, 2016, 04:53am » 

Post deleted

« Last Edit: Sep 15^{th}, 2016, 1:46pm by ezprogramming » 
Logged




zzz000abc
Full Member
member is offline
Gender:
Posts: 268


Re: Time Calculator
« Reply #10 on: Jul 16^{th}, 2016, 02:52am » 

on Jul 15^{th}, 2016, 01:38am, tsh73 wrote: Code:leapYear = (currantYear MOD 4 = 0) and not((currantYear MOD 100 = 0) and (currantYear MOD 400 <> 0) ) 

[/code] nice logic tsh73 keep it up... p and ~ (q and ~ r ) = p and (~q or r) =(div by 4 ) and (not div by 100 or div by 400)

« Last Edit: Jul 16^{th}, 2016, 04:59am by zzz000abc » 
Logged




ezprogramming
Guest


Re: Time Calculator
« Reply #11 on: Jul 16^{th}, 2016, 04:20am » 

Post deleted

« Last Edit: Sep 15^{th}, 2016, 1:46pm by ezprogramming » 
Logged




zzz000abc
Full Member
member is offline
Gender:
Posts: 268


Re: Time Calculator
« Reply #12 on: Jul 16^{th}, 2016, 04:53am » 

you are right Richard tsh' s function was correct I did not read the full story at leap year calculation modifying my post...

« Last Edit: Jul 16^{th}, 2016, 05:00am by zzz000abc » 
Logged




rcdronan
Guest


Re: Time Calculator
« Reply #13 on: Jul 19^{th}, 2016, 7:29pm » 

Thanks to tsh73 for helping me correct the leap year calculations. These are the updated versions with the leap year calculation problem corrected:
Difference between two dates: Code:'get user input
input "Please type the first date in MM/DD/YYYY format: "; firstDate$
input "Please type the second date in MM/DD/YYYY format: "; secondDate$
'prosess input
firstMonth = val(mid$(firstDate$, 1, 2))
firstDay = val(mid$(firstDate$, 4, 2))
firstYear = val(mid$(firstDate$, 7))
secondMonth = val(mid$(secondDate$, 1, 2))
secondDay = val(mid$(secondDate$, 4, 2))
secondYear = val(mid$(secondDate$, 7))
'set defaults
'gosub [setVariables]
'Start of program
1 currantMonth = firstMonth
currantDay = firstDay
currantYear = firstYear
gosub [setVariables]
if firstMonth = secondMonth and firstYear = secondYear then
total = secondDay  firstDay
goto [finished]
end if
total = daysInMonth  currantDay
currantMonth = currantMonth + 1
gosub [setVariables]
2 if currantYear = secondYear then
'count each month as it goes by to month before second date
for currantMonth = currantMonth to secondMonth  1
gosub [setVariables]
total = total + daysInMonth
next currantMonth
currantMonth = currantMonth + 1
gosub [setVariables]
'count to the days in the last date
total = total + secondDay
goto [finished]
end if
for currantMonth = currantMonth to 12
gosub [setVariables]
total = total + daysInMonth
next currantMonth
''
currantMonth = 1 '
currantDay = 1 ' <new!!!
currantYear = currantYear + 1 '
''
3 gosub [setVariables]
if firstYear < secondYear + 1 then
for currantYear = currantYear to secondYear  1
gosub [setVariables]
total = total + daysInYear
next currantYear
end if
4 for currantMonth = 1 to secondMonth  1
gosub [setVariables]
total = total + daysInMonth
next currantMonth
5 total = total + secondDay
6
[finished]
print "The number of days difference between "; firstDate$; " and "; secondDate$; " is "; total; " days."
'wait for key press
input ""; dummyVar
'end the program
end
'
[setVariables]
'check for bug
if (currantMonth > 12) then curantMonth = currantMonth  12:currantYear = currantYear + 1:currantDay = 1
'calculate leapYear
leapYear = (currantYear MOD 4 = 0) and not((currantYear MOD 100 = 0) and (currantYear MOD 400 <> 0))
'calculate daysInYear
if (leapYear = 0) then daysInYear = 365
if (leapYear = 1) then daysInYear = 366
'calculate daysInMonth
if (currantMonth = 1) then daysInMonth = 31
if (currantMonth = 2) and (leapYear = 0) then daysInMonth = 28
if (currantMonth = 2) and (leapYear = 1) then daysInMonth = 29
if (currantMonth = 3) then daysInMonth = 31
if (currantMonth = 4) then daysInMonth = 30
if (currantMonth = 5) then daysInMonth = 31
if (currantMonth = 6) then daysInMonth = 30
if (currantMonth = 7) then daysInMonth = 31
if (currantMonth = 8) then daysInMonth = 31
if (currantMonth = 9) then daysInMonth = 30
if (currantMonth = 10) then daysInMonth = 31
if (currantMonth = 11) then daysInMonth = 30
if (currantMonth = 12) then daysInMonth = 31
return
Difference between two dates with times of day: Code:'Time Calculator:
'IT WORKS!!!
'Get user inputs:
input "Please type the first time in SS:MM:HH 24 hour format: "; firstTime$
input "Please type the first date in DD/MM/YYYY format: "; firstDate$
input "Please type the second time in SS:MM:HH 24 hour format: "; secondTime$
input "Please type the second date in DD/MM/YYYY format: "; secondDate$
'Sort the input into variables:
firstSecond = val(mid$(firstTime$, 1, 2))
firstMinute = val(mid$(firstTime$, 4, 2))
firstHour = val(mid$(firstTime$, 7))
firstDay = val(mid$(firstDate$, 1, 2))
firstMonth = val(mid$(firstDate$, 4, 2))
firstYear = val(mid$(firstDate$, 7))
secondSecond = val(mid$(secondTime$, 1, 2))
secondMinute = val(mid$(secondTime$, 4, 2))
secondHour = val(mid$(secondTime$, 7))
secondDay = val(mid$(secondDate$, 1, 2))
secondMonth = val(mid$(secondDate$, 4, 2))
secondYear = val(mid$(secondDate$, 7))
'Set up the currant variables
currantSecond = firstSecond
currantMinute = firstMinute
currantHour = firstHour
currantDay = firstDay
currantMonth = firstMonth
currantYear = firstYear
'Set variable defaults:
difference = 0
'Seconds:
if currantMinute = secondMinute and currantHour = secondHour and currantDay = secondDay and currantMonth = secondMonth and currantYear = secondYear then
difference = secondSecond  currantSecond
goto [end]
end if
'Work out how many seconds there are to the next minute:
difference = difference + (60  currantSecond)
currantMinute = currantMinute + 1
if currantMinute = 60 then currantMinute = 0:currantHour = currantHour + 1
'Minutes:
if currantHour = secondHour and currantDay = secondDay and currantMonth = secondMonth and currantYear = secondYear then
difference = difference + ((secondMinute  currantMinute) * 60)
currantHour = currantHour + 1
if currantHour = 24 then currantHour = 0:currantDay = currantDay + 1
goto [nextSecond]
end if
'Work out how many minutes there are to the next hour and times it by how many seconds there are in a minute:
difference = difference + ((60  currantMinute) * 60)
currantHour = currantHour + 1
if currantHour = 24 then currantHour = 0:currantDay = currantDay + 1
'Hours:
if currantDay = secondDay and currantMonth = secondMonth and currantYear = secondYear then
difference = difference + ((secondHour  currantHour) * 60 * 60)
currantDay = currantDay + 1
gosub [setDaysInMonthAndYear]
if currantDay > daysInMonth then currantDay = currantDay  daysInMonth:currantMonth = currantMonth + 1
goto [nextMinute]
end if
'Work out how many hours there are to the next day and times it by how many seconds there are in an hour:
difference = difference + ((24  currantHour) * 60 * 60)
currantDay = currantDay + 1
gosub [setDaysInMonthAndYear]
if currantDay > daysInMonth then currantDay = currantDay  daysInMonth:currantMonth = currantMonth + 1
'Days:
if currantMonth = secondMonth and currantYear = secondYear then
difference = difference + ((secondDay  currantDay) * 60 * 60 * 24)
currantMonth = currantMonth + 1
gosub [setDaysInMonthAndYear]
if currantMonth > 12 then currantMonth = currantMonth  12:currantYear = currantYear + 1
goto [nextHour]
end if
'Work out how many days there are to the next month and times it by how many seconds there are in a day:
difference = difference + ((daysInMonth  currantDay) * 60 * 60 * 24)
currantMonth = currantMonth + 1
gosub [setDaysInMonthAndYear]
if currantMonth > 12 then currantMonth = currantMonth  12:currantYear = currantYear + 1
'Months:
if currantYear = secondYear then
for currantMonth = currantMonth to secondMonth
gosub [setDaysInMonthAndYear]
difference = difference + (daysInMonth * 60 * 60 * 24)
next currantMonth
goto [nextDay]
end if
'Work out how many months there are to the next year and times it by how many seconds there are in the individual months:
for currantMonth = currantMonth to 12
gosub [setDaysInMonthAndYear]
difference = difference + (daysInMonth * 60 * 60 * 24)
next currantMonth
currantMonth = 1
currantYear = currantYear + 1
'Years:
'Work out how many years there are to the year before the second year and times it by how many seconds there are in the individual years:
gosub [setDaysInMonthAndYear]
for currantYear = currantYear to secondYear  1
gosub [setDaysInMonthAndYear]
difference = difference + (daysInYear * 60 * 60 * 24)
next currantYear
currantYear = currantYear + 1
[nextMonth]
'Months:
'Work out how many months there are to the month before the second month and times it by how many seconds there are in the individual months:
for currantMonth = 1 to secondMonth  1
gosub [setDaysInMonthAndYear]
difference = difference + (daysInMonth * 60 * 60 * 24)
next currantMonth
currantMonth = currantMonth + 1
[nextDay]
'Days:
'Work out how many days there are to the day before the second day and times it by how many seconds there are in a day:
difference = difference + (secondDay * 60 * 60 * 24)
currantDay = currantDay + 1
[nextHour]
'Hours:
'Work out how many hours there are to the hour before the second hour and times it by how many seconds there are in an hour:
difference = difference + (secondHour * 60 * 60)
currantHour = currantHour + 1
[nextMinute]
'Minutes:
'Work out how many minutes there are to the minute before the second minute and times it by how many seconds there are in a minute:
difference = difference + (secondMinute * 60)
currantMinute = currantMinute + 1
[nextSecond]
'Seconds:
'Work out how many seconds there are to the second second:
difference = difference + (secondSecond)
[end]
'Display output:
print "The difference between "; firstTime$; " "; firstDate$; " and "; secondTime$; " "; secondDate$; " is "; difference; " seconds."
'Wait
input ""; dummyVar$
end
[setDaysInMonthAndYear]
'calculate leapYear
leapYear = (currantYear MOD 4 = 0) and not((currantYear MOD 100 = 0) and (currantYear MOD 400 <> 0))
'calculate daysInYear
if (leapYear = 0) then daysInYear = 365
if (leapYear = 1) then daysInYear = 366
'calculate daysInMonth
if (currantMonth = 1) then daysInMonth = 31
if (currantMonth = 2) and (leapYear = 0) then daysInMonth = 28
if (currantMonth = 2) and (leapYear = 1) then daysInMonth = 29
if (currantMonth = 3) then daysInMonth = 31
if (currantMonth = 4) then daysInMonth = 30
if (currantMonth = 5) then daysInMonth = 31
if (currantMonth = 6) then daysInMonth = 30
if (currantMonth = 7) then daysInMonth = 31
if (currantMonth = 8) then daysInMonth = 31
if (currantMonth = 9) then daysInMonth = 30
if (currantMonth = 10) then daysInMonth = 31
if (currantMonth = 11) then daysInMonth = 30
if (currantMonth = 12) then daysInMonth = 31
return


Logged




rcdronan
Guest


Re: Time Calculator
« Reply #14 on: Oct 16^{th}, 2016, 5:07pm » 

Oh, and I forgot to mention that it doesn't account for daylight saving.


Logged




