1

I am trying to create an environment variable for yesterday's date. It MUST be in the format of MM/DD/YYYY.

For code I currently have:

 set m=%date:~-7,2%
 set /A m -= 1
 set DATE_YESTERDAY=%date:~-10,2%/%m%/%date:~-4,4%
 echo %DATE_YESTERDAY%

This works great, HOWEVER, when I offset to yesterdays day with -1 instead of "12/03/2019" I get "12/3/2019". Thus, missing the zero/0.

Any ideas to keep format as MM/DD/YYYY?

I have seen many other questions regarding this, however, all come short!

NOTE - I do NOT want powershell or VBS scripts. I know this can be done with simply batch.

T-Diddy
  • 125
  • 1
  • 12
  • What if it is the first of a month? you will receive the zeroth then... – aschipfl Dec 05 '19 at 22:15
  • I agree with the above comment, this code is only useful if never run on the first of a month. You're technically asking for a fix for something that will already fail on over 3% of the times it is used, _unless it is never run on the 1st of a month!_ – Compo Dec 07 '19 at 09:56

2 Answers2

2

What you should do is use a method which is not based upon user/locale/PC settings. Commonly is used for that, but it isn't the fastest method and would still require performing some math.

You could use embedded directly into your :

<!-- :
@Echo Off
For /F %%# In ('CScript //NoLogo "%~f0?.wsf"') Do Set "YesterDate=%%#"
Echo(%YesterDate%
Pause
GoTo :EOF
-->
<Job><Script Language="VBScript">
    dtmYesterday = DateAdd("d", -1, Now())
    strDate = Right("0" & Month(dtmYesterday), 2) _
      & "/" & Right("0" & Day(dtmYesterday), 2) _
      & "/" & Year(dtmYesterday)
    WScript.Echo strDate
  </Script></Job>


You could also, if you prefer it, use from your instead:
@Echo Off
For /F %%# In ('PowerShell -NoP "(Get-Date).AddDays(-1).ToString('MM/dd/yyy')"'
)Do Set "YesterDate=%%#"
Echo(%YesterDate%
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • `Echo(%YesterDate%` does not fail. Why is that? This is the first time I've seen un-balance parentheses not fail. – somebadhat Dec 05 '19 at 02:03
  • @somebadhat, there are no unbalanced parentheses in my code. The opening parenthesis directly following `Echo` is not read as a character, it is simply the best character to use to prevent an 'echo is off' error, should the variable be undefined, _(empty)_. Most people tend to use a period, instead of the opening parenthesis, but it has been previously determined that the opening parenthesis is the most robust character to use, _(I often use `=` as an alternative, to prevent the misunderstanding you have made)_. – Compo Dec 05 '19 at 02:10
  • I am trying to understand this: You would get an `echo is off` error if `yesterdate` does not exist? – somebadhat Dec 05 '19 at 02:35
  • Yes, if the [tag:vbscript] or [tag:powershell] command should fail to work, `YesterDate` wouldn't be defined, therefore, `Echo %YesterDate%` would simply read `Echo`. `Echo` is a command, which without an argument, will display the current `Echo` status. – Compo Dec 05 '19 at 02:51
  • That is what I thought. With your code as written how could it fail? And if it did fail wouldn't you want some indication it had? – somebadhat Dec 05 '19 at 03:02
  • Here's a few, @somebadhat...`powershell` or `cscript` may not exist in the current directory or in whatever locations make up the value of the `%PATH%` variable. The running of either of those executables, could be restricted by policy. The `%PATHEXT%` variable value has been modified, intentionally or otherwise and no longer holds `.EXE` as one of its values. There would be an indication of failure, I have not suppressed StdErr and used `Pause` so that the `cmd.exe` window remains open, awaiting a response. – Compo Dec 05 '19 at 09:41
1

Windows command processor cmd.exe does not have built-in support for date calculations. Other script interpreters installed by default on Windows like VBScript or PowerShell support date calculations and Compo posted solutions using VBScript or PowerShell.

Here is a pure batch file solution to calculate yesterday's date from current date with remarks explaining the code. The lines with remark command rem can be removed for faster processing the batch file by Windows command processor.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" (
    rem Get local date and time in a region independent format.
    for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime /VALUE') do set "LocalDateTime=%%I"
) else (
    rem This is for fast testing determining the date of yesterday from any
    rem date specified as parameter in format yyyyMMdd on calling this batch
    rem file from within a command prompt window. The parameter string is
    rem not validated at all as this is just for testing the code below.
    set "LocalDateTime=%~1"
)

rem Get day, month and year from the local date/time string (or parameter).
set "Day=%LocalDateTime:~6,2%"
set "Month=%LocalDateTime:~4,2%"
set "Year=%LocalDateTime:~0,4%"

rem Define a variable with today's date in format MM/dd/yyyy.
set "Today=%Month%/%Day%/%Year%"

rem Decrease the day in month by 1 in any case.

rem It is necessary to remove leading 0 for the days 08 and 09 as
rem those two days would be otherwise interpreted as invalid octal
rem numbers and decreased result would be -1 instead of 7 and 8.
rem if "%Day:~0,1%" == "0" set "Day=%Day:~1%"
rem set /A Day-=1

rem Faster is concatenating character 1 with the day string to string
rem representing 101 to 131 and subtract 101 to decrease day by one.
set /A Day=1%Day%-101

rem The yesterday's date is already valid if the day of month is greater 0.
if %Day% GTR 0 goto BuildYesterday

rem Yesterday is in previous month if day is equal (or less than) 0.
rem Therefore decrease the current month by one with same method as
rem described above to decrease correct also the months 08 and 09.
set "Day=31"
set /A Month=1%Month%-101

rem Yesterday is in previous year if month is equal (or less than) 0.
if %Month% GTR 0 goto GetLastDay
set /A Year-=1
set "Month=12" & goto BuildYesterday

:GetLastDay
rem Determine last day of month depending on month.
for %%I in (4 6 9 11) do if %Month% == %%I set "Day=30" & goto BuildYesterday
if not %Month% == 2 goto BuildYesterday

rem Determine if this year is a leap year with 29 days in February.
set /A LeapYearRule1=Year %% 400
set /A LeapYearRule2=Year %% 100
set /A LeapYearRule3=Year %% 4

rem The current year is always a leap year if it can be divided by 400
rem with 0 left over (1600, 2000, 2400, ...). Otherwise if the current
rem year can be divided by 100 with 0 left over, the current year is NOT
rem a leap year (1900, 2100, 2200, 2300, 2500, ...). Otherwise the current
rem year is a leap year if the year can be divided by 4 with 0 left over.
rem Well, for the year range 1901 to 2099 just leap year rule 3 would be
rem enough and just last IF condition would be enough for this year range.

set "Day=28"
if LeapYearRule1 == 0 goto BuildYesterday
if NOT %LeapYearRule2% == 0 if %LeapYearRule3% == 0 set "Day=29"

rem The leading 0 on month and day in month could be removed and so both
rem values are defined again as string with a leading 0 added and next just
rem last two characters are kept to get day and month always with two digits.

:BuildYesterday
set "Day=0%Day%"
set "Day=%Day:~-2%"
set "Month=0%Month%"
set "Month=%Month:~-2%"

rem Define a variable with yesterday's date in format MM/dd/yyyy.
set "Yesterday=%Month%/%Day%/%Year%"

echo     Today is: %Today%
echo Yesterday is: %Yesterday%

endlocal

Please read my answer on Why does %date% produce a different result in batch file executed as scheduled task? It explains in full details the FOR command line using WMIC to get current date in region independent format.

Here is the code above without comments, empty lines and first IF condition needed only for testing the code. The leap year identification is also optimized for using only third rule which means this code is working only for the years 1901 to 2099 which should be enough.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime /VALUE') do set "LocalDateTime=%%I"
set "Day=%LocalDateTime:~6,2%" & set "Month=%LocalDateTime:~4,2%" & set "Year=%LocalDateTime:~0,4%"
echo     Today is: %Month%/%Day%/%Year%
set /A Day=1%Day%-101
if %Day% GTR 0 goto BuildYesterday
set "Day=31"
set /A Month=1%Month%-101
if %Month% GTR 0 goto GetLastDay
set /A Year-=1
set "Month=12" & goto BuildYesterday
:GetLastDay
for %%I in (4 6 9 11) do if %Month% == %%I set "Day=30" & goto BuildYesterday
if not %Month% == 2 goto BuildYesterday
set /A LeapYearRule3=Year %% 4
if %LeapYearRule3% == 0 (set "Day=29") else set "Day=28"
:BuildYesterday
set "Day=0%Day%"
set "Day=%Day:~-2%"
set "Month=0%Month%"
set "Month=%Month:~-2%"
echo Yesterday is: %Month%/%Day%/%Year%
endlocal

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

See also single line with multiple commands using Windows batch file for an explanation of operator & used to specify more than one command on a single command line.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Alright, I am certainly smelling what you're cooking here. Appreciate the detailed explanation and follow without any doubt now. This is awesome! THANK YOU! – T-Diddy Dec 10 '19 at 14:55