![]() |
Forum Index : Microcontroller and PC projects : Number of Days between two dates?
Author | Message | ||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 328 |
I'm going to be really lazy about this one... Anyone have a MMBASIC code segment that calculates the number of days between two dates? I'm working on a count down timer between two dates and my mind keeps giving me a busy signal for this. ![]() Thanks, Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
I posted this code here a couple of years ago. I have made a small change to make it work on MMBasic 4.7. The code was produced well before MMBasic had integers so it looks strange. (most of my code looks strange!) dim jdi as integer Newdate$= date$ mainloop: DOW (Newdate$, weekday, dy$) ' returns day of week as number and string splitdate (Newdate$, day, month, year) toJD (day, month, year, jd) ' returns Julien Day Number jdi = jd ' convert to iteger for printing print Newdate$;" "weekday;" ";dy$ ;" - JD number: ";str$(jdi) input "New Date: ", Newdate$ if Newdate$<>"" then goto mainloop end ' given date as string, returns day, month, year sub splitdate (currentdate$, d, m, y) d= val(mid$(currentdate$,1,2)) m= val(mid$(currentdate$,4,2)) y= val(mid$(currentdate$,7,4)) end sub 'given day, month, year 'dayz=days since 1/1/1900 - agrees with Excel after 1/3/1900 sub toMSday (d, m, y, dayz, dayofyear) dayofyear= d+int((m-1)*30.57+0.5) if m>2 then dayofyear= dayofyear-1 if (y mod 4)>0 then dayofyear= dayofyear-1 endif dayz= int((y-1900)*365.25-0.25)+dayofyear+1 end sub ' given date as string, returns day of week as number and string sub DOW (currentdate$,weekday, dy$) local d, m, y, dayz splitdate (currentdate$, d, m, y) toMSday (d, m, y, dayz, dayofyear) weekday= abs((dayz-1) mod 7) dy$=mid$("SunMonTueWedThuFriSatSun",weekday*3+1,3) end sub ' input day, month, year - returns julien day number sub toJD ( d, m, y, jd) ff = fix((m-14)/12) jd = fix((1461*(y+4800+ff))/4)+fix((367*(m-2-12*ff))/12)-fix((3*( fix(y+4900+ff)/100))/4)+d-32075 end sub 'input julien day - returns day, month, year sub fromJD (jd, d, m, y) local l, n, i, j l = jd + 68569 n = fix(( 4 * l ) / 146097) l = l - fix(( 146097 * n + 3 ) / 4) i = fix(( 4000 * ( l + 1 ) ) / 1461001) l = l - fix(( 1461 * i ) / 4 )+ 31 j = fix(( 80 * l ) / 2447) d = l - fix(( 2447 * j ) / 80) l = fix(j / 11) m = j + 2 - ( 12 * l ) y = 100 * ( n - 49 ) + i + l end sub You will need to convert to JD and back after doing the maths. Jim VK7JH MMedit |
||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 328 |
Thanks Jim! I guess I'm just been spoiled by how simple this is to do in Liberty BASIC with it's built-in date functions. --Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Except that Liberty Basic wants the date in that strange 'American' order of month,day,year and I have to keep putting it into the correct order for this part of the world. In LB, the file date format depends on the users regional settings so it can be trick to get it correct. I have to resort to the API. I am glad that Geoff got things right! Jim VK7JH MMedit |
||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 328 |
Now you did it... I promised myself I wouldn't get into a date orders controversy. What do mean? Month/Day/Year is the perfect order! All other date formats should be universally banned! ![]() But seriously, I okay with the European format, but as an American I would much rather use MM/DD/YY. I always thought Liberty BASIC supported the other format, but I see now it's really just variations of the MM/DD/YY theme. --Curtis Okay guys... This is all in fun, let's not start another flaming Date Debate again. I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
CircuitGizmos![]() Guru ![]() Joined: 08/09/2011 Location: United StatesPosts: 1427 |
*cough* ISO8601 *cough* Micromites and Maximites! - Beginning Maximite |
||||
Chris Roper Senior Member ![]() Joined: 19/05/2015 Location: South AfricaPosts: 280 |
That is about as dangerous as mentioning SI Units. ![]() http://caroper.blogspot.com/ |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1593 |
BTW. jim did far more useful date conversion routines. Regards causality ≠ correlation ≠ coincidence |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |