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 : Add days
Author | Message | ||||
Gerad Regular Member Joined: 10/01/2024 Location: GermanyPosts: 42 |
Hello Everybody How can I add days? For example Today's date (03/12/2024) plus 7 days equals (03/19/2024) Who can help or give tips on where I can find information about this problem? Thanks in advance |
||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 4228 |
Hi Gerad, Look at the n=Epoch(DateTime$) function. You change the date/time to a large integer number (seconds since xx), add the 7 days (in seconds), and convert back using DateTime$(n). Regards, Volhout Edited 2024-03-13 01:23 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 9114 |
Tip - look at the epoch function and the datetime$ function |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6786 |
The best way is to set a base date. Then you convert your date to the number of days since the base date. The next step is to add (or subtract) a number of days to (or from) your number of days. Then you convert the base date + resulting number of days to a new date. It sounds like a long way round, but basically you need two functions: Given a date, return the number of days since base date. and Given a number of days, return a date since the base date. Your base date can be anything earlier than the earliest date you will need. Beware of leap years! This is how we used to do it. There are better ways now. Edited 2024-03-13 01:29 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
Gerad Regular Member Joined: 10/01/2024 Location: GermanyPosts: 42 |
many thanks for your help Gerad |
||||
cdeagle Senior Member Joined: 22/06/2014 Location: United StatesPosts: 261 |
' demo_dates.bas ' demonstrate date conversions ' March 2, 2017 ''''''''''''''' print " " print "program demo_dates - demonstrate date conversions" print "=================================================" ' request calendar date (month, day, year) getdate(month1, day0, year1) ' request time (hours, minutes, seconds) gettime(thr1, tmin1, tsec1) ' compute "complete" calendar day day1 = day0 + thr1 / 24.0 + (tmin1 / 60.0) / 24.0 + (tsec1 / 3600.0) / 24.0 ' compute corresponding Julian day julian (month1, day1, year1, jday1) ' print results print " " print "convert calendar date and time to julian day" print "--------------------------------------------" print " " print "calendar month = ", month1 print "calendar day = ", day0 print "calendar year = ", year1 print " " print "time = ", str$(thr1) + " hr " + str$(tmin1) + " min " + str$(tsec1, 0, 4) + " sec" print " " print "jday = ", str$(jday1, 0, 9) print " " print "convert Julian day to Gregorian date and time" print "---------------------------------------------" print " " print "jday = ", str$(jday1, 0, 9) ' compute calendar date gdate (jday1, month2, day2, year2) print " " print "calendar month = ", month2 print "calendar day = ", int(day2) print "calendar year = ", year2 print " " thr0 = 24.0 * (day2 - int(day2)) thr2 = int(thr0) tmin0 = 60.0 * (thr0 - thr2) tmin2 = int(tmin1) tsec2 = 60.0 * (tmin0 - tmin2) print "time = ", str$(thr2) + " hr " + str$(tmin2) + " min " + str$(tsec2, 0, 4) + " sec" print " " print "number of days between two dates" print "--------------------------------" print " " print "first date" getdate(month3, day3, year3) julian (month3, day3, year3, jday3) print " " print "second date" getdate(month4, day4, year4) julian (month4, day4, year4, jday4) delta_days = jday4 - jday3 print " " print "number of days between dates = ", delta_days print " " end ''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''' sub julian (month, day, year, jday) ' Gregorian date to julian day subroutine ' input ' month = calendar month ' day = calendar day ' year = calendar year (all four digits) ' output ' jday = julian day ' special notes ' (1) calendar year must include all digits ' (2) will report October 5, 1582 to October 14, 1582 ' as invalid calendar dates and exit ''''''''''''''''''''''''''''''''''''''''' y = year m = month b = 0.0 c = 0.0 if (m <= 2.0) then y = y - 1.0 m = m + 12.0 end if if (y < 0.0) then c = -0.75.0 if (year < 1582.0) then ' null elseif (year > 1582.0) then a = fix(y / 100.0) b = 2.0 - a + fix(a / 4.0) elseif (month < 10.0) then ' null elseif (month > 10.0) then a = fix(y / 100.0) b = 2.0 - a + fix(a / 4.0) elseif (day <= 4.0) then ' null elseif (day > 14.0) then a = fix(y / 100.0) b = 2.0 - a + fix(a / 4.0) else print "this date does not exist!!" exit end if jday = fix(365.25 * y + c) + fix(30.6001 * (m + 1.0)) + day + b + 1720994.5 end sub '''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''' sub gdate (jday, month, day, year) ' Julian day to Gregorian date subroutine ' input ' jday = julian day ' output ' month = calendar month ' day = calendar day ' year = calendar year '''''''''''''''''''''''' z = fix(jday + 0.5) f = jday + 0.5 - z if (z < 2299161) then a = z else alpha = fix((z - 1867216.25) / 36524.25) a = z + 1.0 + alpha - fix(alpha / 4.0) end if b = a + 1524.0 c = fix((b - 122.1) / 365.25) d = fix(365.25 * c) e = fix((b - d) / 30.6001) day = b - d - fix(30.6001 * e) + f if (e < 13.5) then month = e - 1.0 else month = e - 13.0 end if if (month > 2.5) then year = c - 4716.0 else year = c - 4715.0 end if end sub '''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''' sub getdate (month, day, year) ' request calendar date subroutine do print " " print "please input the calendar date" print " " print "(month [1 - 12], day [1 - 31], year [yyyy])" print "< for example, october 21, 1986 is input as 10,21,1986 >" print "< b.c. dates are negative, a.d. dates are positive >" print "< the day of the month may also include a decimal part >" print " " input month, day, year loop until (month >= 1 and month <= 12) and (day >= 1 and day <= 31) end sub ''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''' sub gettime (thr, tmin, tsec) ' request time subroutine do print " " print "please input the time" print " " print "(hours [0 - 24], minutes [0 - 60], seconds [0 - 60])" print " " input thr, tmin, tsec loop until (thr >= 0.0 and thr <= 24.0 and tmin >= 0.0 and tmin <= 60.0 and tsec >= 0.0 and tsec <= 60.0) end sub |
||||
barewires Newbie Joined: 13/04/2015 Location: United KingdomPosts: 30 |
Decades ago I used to type in Sky and Telescope magazine BASIC code for astronomy. https://skyandtelescope.org/astronomy-resources/basic-programs-from-sky-telescope/ Simply remove the line numbers in a .CSV file and fix the gosub, goto and labels etc. > run Enter your birthday (M,D,Y)? 4,2,1950 Enter desired age in days ? 27008 You will be 27008 days old on 3 12 2024 Verify with: https://www.timeanddate.com/date/durationresult.html?d1=02&m1=04&y1=1950&d2=&m2=&y2= Rem DAYSOLD.BAS Input "Enter your birthday (M,D,Y)";M,D,Y Rem Compute Julian Day Number J=-Int(7*(Int((M+9)/12)+Y)/4) S=Sgn(M-9) A=Abs(M-9) J1=Int(Y+S*Int(A/7)) J1=-Int((Int(J1/100)+1)*3/4) J=J+Int(275*M/9)+D+J1 J=J+1721028+367*Y G110: Input "Enter desired age in days ";A9 If A9=0 Then G280 Rem Compute calendar date from new JD number J2=J+A9+1 A1=Int((J2/36524.25)-51.12264) A=J2+1+A1-Int(A1/4) B=A+1524 C=Int((B/365.25)-0.3343) D=Int(365.25*C) E=Int((B-D)/30.61) D=B-D-Int(30.61*E) M=E-1 Y=C-4716 If E>13.5 Then M=M-12 If M<2.5 Then Y=Y+1 Print "You will be "; A9; " days old on " ;M;D;Y GoTo G110 G280: End Rem --------------------------- Rem APPEARED IN COMPUTERS IN Rem ASTRONOMY, SKY & TELESCOPE, Rem NOVEMBER 2001, PAGE 63 Rem --------------------------- Edited 2024-03-13 07:07 by barewires |
||||
barewires Newbie Joined: 13/04/2015 Location: United KingdomPosts: 30 |
https://skyandtelescope.org/wp-content/uploads/calendar.bas Rem PERPETUAL GREGORIAN CALENDAR Rem Dim C$(42), D$(31), E(12) For I=1 To 31: Read D$(I): Next I For I=1 To 12: Read E(I): Next I Data " 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9"," 10" Data " 11"," 12"," 13"," 14"," 15"," 16"," 17"," 18"," 19"," 20" Data " 21"," 22"," 23"," 24"," 25"," 26"," 27"," 28"," 29"," 30"," 31" Data 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 G28: Print : Input "MONTH, YEAR";M,Y: If Y<100 Then Y=Y+1900 Print : Print " SU MO TU WE TH FR SA": Print J=367*Y-Int(7*(Y+Int((M+9)/12))/4)+Int(275*M/9)+1721031 K=0: If M<=2 Then K=-1 J=J-Int(3*(Int((Y+K)/100)+1)/4) K=E(M): If M<>2 Then GoTo G48 W=Int(Y-100*Int(Y/100)): X=Int(Y-4*Int(Y/4)): Z=Int(Y-400*Int(Y/400)) If X<>0 Then GoTo G48 If W=0 And Z<>0 Then GoTo G48 K=29 G48: X=J-7*Int(J/7) For I=1 To 42: C$(I)=" ": Next I For I=1 To K: C$(I+X)=D$(I): Next I For I=1 To 6: J=7*I Print C$(J-6);C$(J-5);C$(J-4);C$(J-3);C$(J-2);C$(J-1);C$(J) Next I Print : Input "ANOTHER";A$: If A$="Y" Then G28 End Rem --------------------------------------------------------------- Rem APPEARED IN ASTRONOMICAL COMPUTING, SKY & TELESCOPE, JULY, 1985 Rem --------------------------------------------------------------- [CODE/] |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6098 |
In most versions of MMBasic you can keep it simple: ' startDate$ = "02-04-1950" startDate$ = startDate$+" 00:00:00" DaysToAdd = 27008 EndDate$= DATETIME$(EPOCH(startDate$) + DaysToAdd*86400) PRINT EndDate$ as suggested in the first two replies. Jim Edited 2024-03-13 11:55 by TassyJim VK7JH MMedit MMBasic Help |
||||
homa Guru Joined: 05/11/2021 Location: GermanyPosts: 351 |
great Jim Matthias |
||||
Gerad Regular Member Joined: 10/01/2024 Location: GermanyPosts: 42 |
Hello Jim, thank you very much for the program example. It solved my little problem immediately. Greetings to Australia. I would also like to thank everyone else for their help Gerad |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2075 |
always check the libraries https://tbsassist.com/wiki/doku.php?id=mmbasic:dateadd_function_vb_work_a_like |
||||
disco4now Guru Joined: 18/12/2014 Location: AustraliaPosts: 896 |
Hi Andrew, Good to see you making a couple of posts. The original fruitoftheshed.com domain was successfully transfer to the new site and registered for the next six years so will live on with its original URL. https://fruitoftheshed.com/wiki/doku.php?id=mmbasic:dateadd_function_vb_work_a_like Regards Gerry Latest F4 Latest H7 |
||||
Print this page |