1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? Find Previous Day Subroutine

Discussion in 'T&T - Miscellaneous' started by scottvk, Aug 12, 2016.

  1. scottvk

    Joined:
    Aug 12, 2016
    Messages:
    2
    Likes Received:
    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
     
  2. TimButterfield

    Joined:
    May 20, 2008
    Messages:
    401
    Likes Received:
    1
    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.
     
  3. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    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]]]
    
     
  4. scottvk

    Joined:
    Aug 12, 2016
    Messages:
    2
    Likes Received:
    0
    Thanks, that's good to know.
     
  5. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    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
    
     

Share This Page