How to? Find Previous Day Subroutine

Aug 12, 2016
2
0
Below is a set of TCC subroutines that will find the previous day
  • Finds last day of previous month if necessary
  • Accounts for 1st day of year
  • Accounts for Leap Year
GOSUB GET_DATE
echo input date is %inputMonth%/%inputDay%/%inputYear%
EXIT

REM *** SUBROUTINES GO BELOW ***
:GET_DATE
set inputYear=%@substr[%@dateconv[%date,4],0,4]
set inputMonth=%@substr[%@dateconv[%date,4],5,2]
set inputDay=%@substr[%@dateconv[%date,4],8,2]
GOSUB DETERMINE_PREVIOUS_DAY​
RETURN
:DETERMINE_PREVIOUS_DAY
REM SUBTRACT 1 FROM CURRENT DAY
SET inputDay=%@eval[%inputDay%-1]
IFF %inputDay == 0 THEN & set inputMonth=%@eval[%inputMonth%-1]
GOSUB DETERMINE_LAST_DAY_OF_PREVIOUS_MONTH
&ENDIFF​
RETURN
:DETERMINE_LAST_DAY_OF_PREVIOUS_MONTH
IFF %inputMonth == 0 THEN & set inputMonth=12 & set inputYear=%@eval[%inputYear-1] &ELSEIFF %inputMonth LT 10 THEN & set inputMonth=0%inputMonth% &ENDIFF
REM
echo input MM/Day is %inputMonth%/%inputDay
REM 31 days in month - Jan,Mar,May,Jul,Aug,Oct,Nov
REM @WILD[string1,string2] : Compares two strings and returns 1 if they match or 0 if they don't match. **Does *NOT* work with Variable in string 1**
IFF %@WILD[01030507081012,*%inputMonth%*] EQ 1 THEN & SET inputDay=31
REM 30 days in month - Apr,Jun,Sep,Nov
ELSEIFF %@WILD[04060911,*%inputMonth%*] EQ 1 THEN & SET inputDay=30
REM Determine if last day of February is 28 or 29th (due to leap year)
ELSE GOSUB FEBRUARY_PROC
ENDIFF​
RETURN
REM
REM Check if date is Leap Year (year is divisible by 4 *AND* (Year is *NOT* divisible 100 OR Year *IS* divisible 100 & also 400))
:FEBRUARY_PROC
IFF %@eval[%inputYear MOD 4] != 0 THEN &(SET inputDay=28)
&ELSEIFF %@eval[%inputYear MOD 100] != 0 THEN &(SET inputDay=29)
&ELSEIFF %@eval[%inputYear MOD 400] = 0 THEN &(SET inputDay=29)&ELSE& SET inputDay=28
&ENDIFF​
RETURN
 
May 20, 2008
457
2
Here is a shorter way of getting the last day of the prior month (relative to the current date month):
Code:
echo %@makedate[%@eval[%@date[%_month/1/%_year]-1]]
You can make that an alias and then wrap in @execstr[...] to use as a function. If you want to pass an input date, you can get that month with %@month[%InputDate] or use %@if to make the InputDate optional.
 

samintz

Scott Mintz
May 20, 2008
1,324
12
Solon, OH, USA
If all you want is the date of the previous day, then why not just do:
Code:
echo %@agedate[%@eval[%@makeage[%_date] - 864000000000]]
or 
echo %@makedate[%@dec[%@date[%_date]]]
 

samintz

Scott Mintz
May 20, 2008
1,324
12
Solon, OH, USA
If all you want is the date of the previous day, then why not just do:
Code:
echo %@agedate[%@eval[%@makeage[%_date] - 864000000000]]
or
echo %@makedate[%@dec[%@date[%_date]]]
You can specify your own dates also instead of using %_date.

Code:
function prevday=`%@makedate[%@dec[%@date[%1]]]`
echo %@prevday[%_date]
08/11/16

echo %@prevday[3/1/16]
02/29/16