Wednesday, November 16, 2016

Windows Batch Scripting: Get the previous date of a given date


Hello everyone
It’s been great working with windows batch scripting. Just to get all of us familiar with windows batch scripting, let us take up a small date play activity.
Let’s name the activity as “GetPreviousDateInteractive”.
The intention is to create a batch file which
1.       Displays the previous date immediately when opened
2.       Interacts with us asking if we wanna play more
3.       If yes, then asks us to enter year, month and date and gives us the previous date
4.       If no, it simply exits
Note you that this is not some kind of a random pick but the very idea of the activity has its vitality in most of the requirements playing with the date. I therefore believe that this would be useful to us from learning perspective and boosts our confidence to do a hands on work.

@ECHO OFF
REM - Grab the year, month and date from the system date
SET /A y=%date:~10,4%  
SET /A m=%date:~4,2%
SET /A d=%date:~7,2%

SET /A origdate=d
REM - If the system date is January first, set year to previous year
IF %d%==1 IF %m% ==1 SET /A y-=1
REM – If month of the system date is greater than January, then set month to previous month
IF %d%==1 IF %m% GTR 01 SET /A m-=1
REM – If month of the system date is January, then set month to december
IF %d%==1 IF %m% ==1 SET /A m=12

REM – Calculate the modulus of the year to determine if it is a leap year or not
SET /A  rmdr=(y%%4+4)%%4

REM – If the date is first of a month set the last date of previous month
IF %d%==1 IF %m%==1 SET /A d=31
IF %d%==1 IF %m%==2 IF %rmdr%==0 SET /A d=29 REM – If the modulus calculated above is zero, the year is leap, so february will have 29 days
IF %d%==1 IF %m%==2 IF %rmdr% NEQ 0 SET /A d=28 REM – If the modulus calculated above is not zero, the year is not leap, so february will have 28 days
IF %d%==1 IF %m%==3 SET /A d=31
IF %d%==1 IF %m%==4 SET /A d=30
IF %d%==1 IF %m%==5 SET /A d=31
IF %d%==1 IF %m%==6 SET /A d=30
IF %d%==1 IF %m%==7 SET /A d=31
IF %d%==1 IF %m%==8 SET /A d=31
IF %d%==1 IF %m%==9 SET /A d=30
IF %d%==1 IF %m%==10 SET /A d=31
IF %d%==1 IF %m%==11 SET /A d=30
IF %d%==1 IF %m%==12 SET /A d=31

REM – If the date is not equal to one, reduce the date by one
IF %origdate% NEQ 1 SET /A d=%origdate%-1
REM – If the month or date is less than  ten, append a zero at the beginning, so that there will be a two digit format
IF %m% LSS 10 set m=0%m%
IF %d% LSS 10 set d=0%d%

REM – Recreate the previous date with the year_month_date attributes
SET timeperiod=%y%_%m%_%d%
ECHO Yesterday's date is %timeperiod%

REM – Make the script more interactive with the labels and go to in the below code
:Play
SET /p q=Do you wanna play more?
IF %q%==yes GOTO :MorePlay
IF %q%==Yes GOTO :MorePlay
IF %q%==s GOTO :MorePlay
IF %q%==S GOTO :MorePlay
IF %q%==No GOTO :EOF
IF %q%==no GOTO :EOF
IF %q%==N GOTO :EOF
IF %q%==n GOTO :EOF

:MorePlay
SET /p y=Enter the year:
SET /p m=Enter the month:
SET /p d=Enter the date:

SET /A y=%y%
SET /A m=%m%
SET /A d=%d%

SET /A origdate=d

IF %d%==1 IF %m% ==1 SET /A y-=1
IF %d%==1 IF %m% GTR 01 SET /A m-=1
IF %d%==1 IF %m% ==1 SET /A m=12

SET /A  rmdr=(y%%4+4)%%4

IF %d%==1 IF %m%==1 SET /A d=31
IF %d%==1 IF %m%==2 IF %rmdr%==0 SET /A d=29
IF %d%==1 IF %m%==2 IF %rmdr% NEQ 0 SET /A d=28
IF %d%==1 IF %m%==3 SET /A d=31
IF %d%==1 IF %m%==4 SET /A d=30
IF %d%==1 IF %m%==5 SET /A d=31
IF %d%==1 IF %m%==6 SET /A d=30
IF %d%==1 IF %m%==7 SET /A d=31
IF %d%==1 IF %m%==8 SET /A d=31
IF %d%==1 IF %m%==9 SET /A d=30
IF %d%==1 IF %m%==10 SET /A d=31
IF %d%==1 IF %m%==11 SET /A d=30
IF %d%==1 IF %m%==12 SET /A d=31

IF %origdate% NEQ 1 SET /A d=%origdate%-1
IF %m% LSS 10 set m=0%m%
IF %d% LSS 10 set d=0%d%

SET timeperiod=%y%_%m%_%d%
ECHO Previous Day Date is %timeperiod%

GOTO :Play

Try this yourself. Meet you in the next post.

Thanks
SunilDutt N

No comments:

Post a Comment