Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:08 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Number of Days between two dates?

Author Message
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 328
Posted: 04:31pm 27 Oct 2015
Copy link to clipboard 
Print this post

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: Australia
Posts: 6283
Posted: 04:52pm 27 Oct 2015
Copy link to clipboard 
Print this post

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 States
Posts: 328
Posted: 08:25am 28 Oct 2015
Copy link to clipboard 
Print this post

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: Australia
Posts: 6283
Posted: 11:57am 28 Oct 2015
Copy link to clipboard 
Print this post

  Justplayin said   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

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 States
Posts: 328
Posted: 12:48pm 28 Oct 2015
Copy link to clipboard 
Print this post

  TassyJim said  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.
.
.
.
I am glad that Geoff got things right!

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 States
Posts: 1427
Posted: 05:53am 29 Oct 2015
Copy link to clipboard 
Print this post

*cough* ISO8601 *cough*
Micromites and Maximites! - Beginning Maximite
 
Chris Roper
Senior Member

Joined: 19/05/2015
Location: South Africa
Posts: 280
Posted: 06:12am 29 Oct 2015
Copy link to clipboard 
Print this post

  CircuitGizmos said   *cough* ISO8601 *cough*


That is about as dangerous as mentioning SI Units.
http://caroper.blogspot.com/
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 06:21am 29 Oct 2015
Copy link to clipboard 
Print this post

BTW.

jim did far more useful date conversion routines.

Regards
causality ≠ correlation ≠ coincidence
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025