Search code examples
datebatch-filecmd

Bat How to find the specified date number of weekday?


Any idea how to set compare the date that can find the day of week? how to modify the below coding?

for /f "delims=" %%a in ('wmic path win32_localtime get dayofweek /format:list ') do for /f "delims=" %%d in ("%%a") do set %%d
echo today of the week : %dayofweek%

for example, conditions:

  1. the last day of last month (is not sat or Sun)
  2. checking leap year
@echo off

set Month=%DATE:\~1,1%
set /a "Year=%date:\~6,4%"
set /a "LastMonth=%Month%-1"
set /a "leapyear=%Year% %%4"
set /a Leap="%Year% %% 4"
set Feb=28

if "%Leap%"=="0" set Feb=29
IF %LastMonth%==1 (set LastDay=31)
IF %LastMonth%==2 (set LastDay=%Feb%)
IF %LastMonth%==3 (set LastDay=31)
IF %LastMonth%==4 (set LastDay=30)
IF %LastMonth%==5 (set LastDay=31)
IF %LastMonth%==6 (set LastDay=30)
IF %LastMonth%==7 (set LastDay=31)
IF %LastMonth%==8 (set LastDay=31)
......

Solution

  • If you only want to know the day of the week of the last day of the previous month (for me, the last month is December), then you can get it in a very simple way:

    @echo off
    setlocal
    
    for /F "tokens=1-2" %%a in ('wmic path win32_localtime get Day^,DayOfWeek') do (
       set /A "DowLdPm=%%b-%%a%%7, DowLdPm-=7*(DowLdPm>>31)-1" 2> NUL
    )
    echo The last day of the previous month was: %DowLdPm%  (1=Sun..7=Sat)
    

    If you want a prettier output, show the result this way:

    for /F "tokens=%DowLdPm%" %%a in ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do (
       echo The last day of the previous month was: %%a
    )
    

    If you also want the date of such last day of previous month, then the formula is slightly more complicated:

    @echo off
    setlocal
    
    for /F "tokens=1-4" %%a in ('wmic path win32_localtime get Day^,DayOfWeek^,Month^,Year') do (
       set /A "DowLdPm=%%b-%%a%%7, DowLdPm-=7*(DowLdPm>>31)-1, Month=%%c-1, Year=%%d-!Month, Feb=28+!(Year%%4), Month+=12*!Month, MM=100+Month" 2> NUL
    )
    
    for /F "tokens=%DowLdPm%" %%d in ("Sunday Monday Tuesday Wednesday Thrusday Friday Saturday") do (
       for /F "tokens=%Month%" %%l in ("31 %Feb% 31 30 31 30 31 31 30 31 30 31") do (
          echo The last day of the previous month was: %Year%/%MM:~1%/%%l  (%%d^)
       )
    )
    

    You could also solve this problem in a very different way using my printf.exe version 2.11 program.

    My printf.exe application is a Windows console program that is a wrapper for the well-known printf CRT function, thus allowing text and formatted numeric values ​​to be displayed in the cmd.exe window. In addition, my program printf.exe also allows to perform Reverse Polish Notation arithmetic operations on 32-bit integers and 64-bit double floating-point numbers using the same method and functions of the stack-based Hewlett-Packard calculators.

    The new printf.exe version 2.11 also manages character string operations and allows to write scripts (programs) using the simplest programming scheme. The Batch file below contains a printf.exe program that also solves this problem. Note that the printf.exe method uses the same formulae of the previous Batch file.

    @echo off
    setlocal
    
    rem Calculate the day of week of the last day of previous month in Aacini's printf.exe
    rem Antonio Perez Ayala
    
    rem Get date parts in a,b,c,d variables in order to pass they to printf.exe code
    for /F "tokens=1-4" %%a in ('wmic path win32_localtime get Day^,DayOfWeek^,Month^,Year') do (
       set /A "a=%%a, b=%%b, c=%%c, d=%%d" 2> NUL
    )
    
    rem The printf.exe program below uses Integer storage registers this way:
    rem R1..R7:  Pointers to names of days of week: "Sunday" "Monday" ... "Saturday"
    rem R8..R19: Days per month: 31 %Feb% 31 30 31 30 31 31 30 31 30 31
    rem so just R0 is free
    
    printf "The last day of the previous month was: %%i/%%02i/%%i  (%%s)\n" /* Output format    */ ^
        "Sunday Monday Tuesday Wednesday Thrusday Friday Saturday"          /* Enter days names */ ^
        split               /* "Sunday" "Monday" ... "Saturday" 7              Separate names   */ ^
        ]I /" < /"          /* Store the 7 in I and drop it                 */ ^
        (                   /* FOR /L I in (7,-1,1) DO (                    */ ^
           ]i               /*    Store "Saturday","Friday"... in R7,R6...  */ ^
           /" < /"          /*    Drop it                                   */ ^
           ]--I             /*    Decrement I                               */ ^
           [I ==0? ;        /*    Is zero? : QUIT                           */ ^
           /" < /"          /*    Drop the I                                */ ^
        :                   /* REPEAT                                       */ ^
        )                   /* )                                            */ ^
        31 28 31 30 31 30 31 31 30 31 30 31         /* Enter Days Per Month */ ^
        19 ]I /" < /"       /* RI = Index of R19 and drop it                */ ^
        12 ]0 /" < /"       /* R0 = Counter and drop it                     */ ^
        (                   /* FOR /L R0 IN (12,-1,1) DO (                  */ ^
           ]i               /*    Store 31,30,31... in R19,R18,R17...       */ ^
           /" < /"          /*    Drop it                                   */ ^
           ]--I             /*    Decrement I                               */ ^
           ]--0             /*    Decrement R0                              */ ^
           [0 ==0? ;        /*    Is zero? : QUIT                           */ ^
           /" < /"          /*    Drop the R0                               */ ^
        :                   /* REPEAT                                       */ ^
        )                   /* )                                            */ ^
        /" <* /"            /* Drop all                                     */ ^
        c atoi --           /* Month                        Month=%%c-1     */ ^
        d atoi              /* Month %%d                                    */ ^
        /" >2 /"            /* Month %%d Month                              */ ^
        !                   /* Month %%d !Month                             */ ^
        -                   /* Month Year                   Year=%%d-!Month */ ^
        /" > /" 4           /* Month Year Year 4                            */ ^
        (                   /* IF                                           */ ^
           ( %%? )?         /*    Year%4 == 0 ?                             */ ^
           ]++9             /*    Increment days in February (in R9)        */ ^
        )                   /* ENDIF                                        */ ^
        /" < /"             /* Month Year                   Drop Year%4     */ ^
        /" <> /"            /* Year Month                   Exchange        */ ^
        (                   /* IF                                           */ ^
           ==0?             /*    Month == 0 ?                              */ ^
           12 +             /*    Month = 12                                */ ^
        )                   /* ENDIF                                        */ ^
                            /* Year Month                                   */ ^
        /" > /" 7 +         /* Year Month Month+7           = 8..19         */ ^
        ]I /" < /"          /* Year Month                   RI = Month index*/ ^
        [i                  /* Year Month DaysPerMonth                      */ ^
        b atoi              /* Year Month DaysPerMonth %%b                  */ ^
        a atoi              /* Year Month DaysPerMonth %%b %%a              */ ^
        7 %% -              /* Year Month DaysPerMonth DowLdPm=%%b-%%a%%7   */ ^
        /" > 31 >>          /* Year Month DaysPerMonth DowLdPm DowLdPm>>31 "*/ ^
        7 * 1 - -       /"  /* Year Month DaysPerMonth DowLdPm-=7*(DowLdPm>>31)-1"*/ ^
        ]I /" < /"          /* Year Month DaysPerMonth      RI = DowName index */ ^
        [i                  /* Year Month DaysPerMonth "DowName"            */ ^
        OUT                 /*                              Show it!   :)   */
    

    Output:

    The last day of the previous month was: 2023/07/31  (Monday)
    

    Although it may seem complicated, the printf.exe instructions are very simple. HP calculator users can start writing printf.exe programs in minutes after understanding the differences and the programming scheme. You can review a larger example of a printf.exe program in the Base64 decoder

    You can download the printf.exe package from this link