Page 1 of 1

### DateAdd function to Convert to Future Date and Past Date

Posted: Tue 18 Jun 2019, 18:44
Sample to Add days or Substract Days to a Date using a delimit for date.

Code: Select all

``````      INSTALL @lib\$+"DATELIB"
INSTALL @lib\$+"STRINGLIB"

REM Add Future Date
date1\$="01/01/2019"
date2\$=FN_DateAdd(date1\$,15,"/")
PRINT "Add 15 Days to date: "+date1\$+" = "+date2\$

REM Substract date using negative value in days
date1\$="01/01/2019"
date2\$=FN_DateAdd(date1\$,-15,"/")
PRINT "Substract 15 Days to date: "+date1\$+" = "+date2\$

END

DEF FN_DateAdd(date\$,days%,delimit\$)
Day%=VAL(FN_word(date\$,delimit\$,1))
Month%=VAL(FN_word(date\$,delimit\$,2))
Year%=VAL(FN_word(date\$,delimit\$,3))
MJD%=FN_mjd(Day%, Month%, Year%)
MJD%=MJD%+days%
Date2\$=FN_date\$(MJD%,"dd"+delimit\$+"MM"+delimit\$+"yyyy")
=Date2\$

REM This function thanks to Richard Rusell I have include in STRINGLIB.BBC
DEF FN_word(a\$, delimit\$, n%)
PRIVATE a\$(), p\$, c%
IF p\$<>a\$ p\$ = a\$ : c% = FN_split(p\$, delimit\$, a\$())
IF n%<1 OR n%>c% THEN = ""
= a\$(n%-1)

``````

### Re: DateAdd function to Convert to Future Date and Past Date

Posted: Wed 19 Jun 2019, 11:49
DATELIB already contains already the functionality you are looking for without having to use STRINGLIB.
Is this an alternative ? A shorter one ?
Or am I missing something ? (wouldn't be the first time)

Edja

Code: Select all

``````      INSTALL @lib\$+"DATELIB"

REM Add Future Date
date1\$="01/01/2019"
date2\$=FN_DateAdd(date1\$,15,"/")
PRINT "Add 15 Days to date: "+date1\$+" = "+date2\$

REM Substract date using negative value in days
date1\$="01/01/2019"
date2\$=FN_DateAdd(date1\$,-15,"/")
PRINT "Substract 15 Days to date: "+date1\$+" = "+date2\$
END

DEFFN_DateAdd(date\$,days%,dl\$)
=FN_date\$(FN_readdate(date\$,"dmy",0)+days%,"dd"+dl\$+"MM"+dl\$+"yyyy")
``````

### Re: DateAdd function to Convert to Future Date and Past Date

Posted: Wed 19 Jun 2019, 15:46
There is no such function but obviously DATELIB has the necessary functions to obtain it.

### Re: DateAdd function to Convert to Future Date and Past Date

Posted: Wed 19 Jun 2019, 19:29
I did not know it was possible to add or subtract days to the FN_date\$ function with FN_readdate in that way.

FN_date\$ for format and FN_readdate for add or substract days.

Code: Select all

`````` =FN_date\$(FN_readdate(date\$,"dmy",0)+days%,"dd"+dl\$+"MM"+dl\$+"yyyy")
``````
Thanks