Help please - date math

Bish99&@
Posts: 8
Joined: Sun Jan 28, 2018 4:30 am
My devices: iPad pro 10.5", iOS 11.2.5, MacBook Pro
Location: Richmond, Va., U.S.
Flag: United States of America

Help please - date math

Post by Bish99&@ »

I am attempting to use the date functions posted in the forum to help me categorize input based on 7, 14, 30 day intervals. I'm hoping to read an input file with a date field and compare it to today's date in order to find the proper date category. I am not having any luck determining how the interval value is returned from the function. If anyone is able to help me figure this out I would greatly appreciate it.

Thank you,
Steve

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Help please - date math

Post by rbytes »

I have used the current date and time functions, but haven't written anything that required calculating intervals between dates. Dutchman wrote some code to do this in 2014. Take a look and see if it can be adapted for your needs. Here is a link:

viewtopic.php?f=20&t=556

The Forum Search window works quite well. When I need a particular routine, I always type in word combinations and do a search to see if someone else has posted something along the same lines..
The only thing that gets me down is gravity...

Bish99&@
Posts: 8
Joined: Sun Jan 28, 2018 4:30 am
My devices: iPad pro 10.5", iOS 11.2.5, MacBook Pro
Location: Richmond, Va., U.S.
Flag: United States of America

Re: Help please - date math

Post by Bish99&@ »

Thanks for your reply. I have done a search for "date math" but nothing came that matched my requirements. I have been trying to use Dutchman's functions, but as I said I can't figure out how to get/find the interval value returned so I can use it in my program.

Thanks for your time,
Steve

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Help please - date math

Post by Henko »

I once made a number of date/time functions for my own use.
Using some of them, your problem could be solved (imho), using
the following method.
The date format is European, the method is only valid for dates
after january 1st, 2000

Code: Select all

' Test if a given date differs a certain number of days
' with the current date. In this case 7, 14, and 30 days
'
n_today=nod(today$)
input "dd-mm-yyy":date_in$    ' European format
n_input_day=nod(date_in$)
diff=abs(n_input_day-n_today)
if diff%7=0 then print date_in$ & " in 7 days interval"
if diff%14=0 then print date_in$ & " in 14 days interval"
if diff%30=0 then print date_in$ & " in 30 days interval"
end

' current date as dd-mm-yyyy
'
def today$()
y=current_year()
mo=current_month() ! if mo<10 then mo$="0"&mo else mo$=mo
d=current_date()   ! if d<10 then d$="0"&d else d$=d
return d$&"-"&mo$&"-"&y
end def

' nod - number of days after 01 january, 2000
' the date is passed as string
' 
def nod(dt$)
base=option_base()
dim t(12)
if date2n(dt$)=0 then return 0
d=date2n.d ! m=date2n.m ! y=date2n.y
restore to labdata
for i=base to base+11 ! read t(i) ! next i
labdata:
data 0,1,-1,0,0,1,1,2,3,3,4,4
if y>=2000 then y-=2000
nd=365*y+floor((y-1)/4)+30*(m-1)+t(base+m-1)+d
if m>2 and y/4=0 then nd+=1
return nd
end def

' date$ to date numerics
' numerics available as date2date.d, .m and .y
' return 1 if ok, 0 if invalid date-format was passed
' 
def date2n(dt$)
base=mid$("10",1,1)
split dt$ to dat$,nd with "-"
if nd<>3 then return 0
d=dat$(base) ! m=dat$(base+1) ! y=dat$(base+2)
return 1
end def

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Help please - date math

Post by rbytes »

Here is a slight variation on the code. You can enter two dates and get the number of days between them. Both dates
must be after January 1st, 2000.

Code: Select all

/*
Date Calculator by Henko, February 2018
The date format is European. The method is only valid
for dates after January 1st, 2000

Modified by rbytes to give number of days between two dates.
*/

input "First date as dd-mm-yyy":date_in$    ' European format
n_input_day2=nod(date_in$)
input "Second date as dd-mm-yyy":date_in2$    ' European format
n_input_day=nod(date_in2$)
diff=abs(n_input_day-n_input_day2)
print "The difference is";diff;"days"
end

' nod - number of days after 01 january, 2000
' the date is passed as string
' 
def nod(dt$)
base=option_base()
dim t(12)
if date2n(dt$)=0 then return 0
d=date2n.d ! m=date2n.m ! y=date2n.y
restore to labdata
for i=base to base+11 ! read t(i) ! next i
labdata:
data 0,1,-1,0,0,1,1,2,3,3,4,4
if y>=2000 then y-=2000
nd=365*y+floor((y-1)/4)+30*(m-1)+t(base+m-1)+d
if m>2 and y/4=0 then nd+=1
return nd
end def

' date$ to date numerics
' numerics available as date2date.d, .m and .y
' return 1 if ok, 0 if invalid date-format was passed
' 
def date2n(dt$)
base=mid$("10",1,1)
split dt$ to dat$,nd with "-"
if nd<>3 then return 0
d=dat$(base) ! m=dat$(base+1) ! y=dat$(base+2)
return 1
end def
The only thing that gets me down is gravity...

Bish99&@
Posts: 8
Joined: Sun Jan 28, 2018 4:30 am
My devices: iPad pro 10.5", iOS 11.2.5, MacBook Pro
Location: Richmond, Va., U.S.
Flag: United States of America

Re: Help please - date math

Post by Bish99&@ »

Thank you rbytes and henko, I really appreciate your help. I am working on figuring out how to implement your code into a function I can use in my program. Both of you are generous with your help, I doubt I'll get proficient enough but I hope I can help someone in the future.

Steve

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Help please - date math

Post by GeorgeMcGinn »

Why limit the date to one after 1/1/2000 to present.

Below is a function that works for the Gregorian calendar. (People in English-speaking countries used a different calendar before September 14, 1752.)

When writing code, why limit it to 17 years? I know that you are showing it as an example, but if I wanted it to work for my birthday, or I wanted to display the number of days WWII lasted, your example is useless and working in base 11 is beyond most simple programmers.

Here is a more elegant formula that works with any date from 9/14/1752 to the present.

It also takes into consideration years that are and are not leap years.

Enjoy.

EDITED: This formula has been around for awhile, so if you want to read up on it and why it works:

https://alcor.concordia.ca/~gpkatch/gda ... rithm.html
https://alcor.concordia.ca/~gpkatch/gdate-method.html

Code: Select all

PRINT g(2017,2,1) - g(1958,12,2)

DEF g(y,m,d)
    m = (m + 9) % 12
    y = y - m/10
    nd = INT(365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) )
    RETURN nd
ENDDEF

REM Difference between two dates = g(y2,m2,d2) - g(y1,m1,d1)
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Help please - date math

Post by Henko »

Why limit the date to one after 1/1/2000 to present.

Below is a function that works for the Gregorian calendar. (People in English-speaking countries used a different calendar before September 14, 1752.)

When writing code, why limit it to 17 years? I know that you are showing it as an example, but if I wanted it to work for my birthday, or I wanted to display the number of days WWII lasted, your example is useless and working in base 11 is beyond most simple programmers.

Here is a more elegant formula that works with any date from 9/14/1752 to the present.

It also takes into consideration years that are and are not leap years.
Dear George,

Please reread the first thing i said in my message. It said that i made the function "for my own use". At the time i did not need to cover dates before 2000, and i was not aware of the nice function you presented here. I did not upload the function then precisely for that limitation.
So now you know why i made such a limited function, it fullfilled my own needs 100%.

In the mean time, thanks for pointing to the better and even shorter function, it will certainly replace mine in the data library (after testing if it works correctly😉).

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Help please - date math

Post by Henko »

George,

Are you sure about your little code?
In the website you refer to, it is said that all divisions are integer divisions. In your code they are not. There's an INT for the end result, but i think each individual division should be INT'ed.
Also bear in mind that in SB, INT means rounding and not truncation.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Help please - date math

Post by Dutchman »

The program 'Date Calculator.txt' does it all.
'Date Calculator.txt' is a program to calculate periods or dates after certain periods. Its usage is described in the preface of the program.
See viewtopic.php?f=20&t=556
Last edited by Dutchman on Thu Jan 19, 2023 3:04 pm, edited 1 time in total.

Post Reply