0

I current run the below script to get the current day minus 1 and the current month. It works great for all days and month except for the 8th of every month and August of every year. I have to change the script to setting it manually for August. Does anyone know why and is there a fix.

SET m=%date:~4,2%
SET /A m -= 1

SET m=0%m%

REM ****** SET m=08 this was used because the date was not right ******

REM SET m=08

SET currMon=%date:~4,2%/%date:~10,4%

REM ****** SET PriorMon=12/2017 this was used for Year End because the date was not right ******

REM SET PriorMon=08/2018

SET PriorMon=%m:~-2%/%date:~10,4%
3
  • This is not the correct way do get the previous day. Let me post a proper way for you as answer. Commented Sep 7, 2018 at 13:58
  • It also crashes on 09. Plenty of questions and answers on StackOverFlow that are about date math. Commented Sep 7, 2018 at 14:07
  • 1
    Number strings starting with 0 are interpreted as octal numbers. 08 and 09 are invalid values in octal system. For that reason these two invalid values are replaced by 0 on evaluation of the arithmetic expression. There are really dozens of answers showing how to work around unexpected interpretation of a value with a leading 0 as octal number. And there are really dozens of date maths related answers on Stack Overflow, too. It is always a good idea to first research before starting a development and re-invent something which others have already coded perfect and published. Commented Sep 7, 2018 at 14:34

2 Answers 2

1

This is a hybrid vb/batch script. It is a proper way to get the date -1 or whatever amount of days you want:

@echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\*%~n0.vbs"
set "yyyy=%result:~0,4%"
set "mm=%result:~4,2%"
set "dd=%result:~6,2%"
set "final=%dd%-%mm%-%yyyy%"
echo %final%

I simply echo the final result here which as far as today's date goes (for me as it is the 7th) should echo 06-09-2018

You can change the format of %final% as you please to suit your date..

Sign up to request clarification or add additional context in comments.

Comments

0

Proper date calculatons in pure batch are possible but tedious. Your approach relies on possibly unknown locale/user settings dependant date format.

From Win7 on Powershell is available as a tool:

On cmd line:

For /f "usebackq" %A in (`powershell -Nop -C "(Get-Date).AddDays(-1).ToString('MM\/yyyy')"`) Do Set Yesterday=%A

In a batch file:

For /f "usebackq" %%A in (`
    powershell -Nop -C "(Get-Date).AddDays(-1).ToString('MM\/yyyy')"
`) Do Set Yesterday=%%A
Echo Yesterday=%Yesterday%

Modify the format string to your liking:

dd   = day   2 places
MM   = month 2 places
yyyy = year  4 places

Other characters have to be escaped with a backslash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.