知道如何设置比较可以找到星期几的日期吗?如何修改下面的编码?
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%
例如, 条件:
@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)
......
你的问题不够明确...如果你只想知道上月的最后一天是星期几(对我来说,上个月是12月),那么你可以在一个非常简单的方法:
@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)
如果您想要更漂亮的输出,请这样显示结果:
for /F "tokens=%DowLdPm%" %%a in ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do (
echo The last day of the previous month was: %%a
)
如果您还想要上个月最后一天的日期,那么公式会稍微复杂一些:
@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^)
)
)
您还可以使用我的 printf.exe 版本 2.11 程序以非常不同的方式解决此问题。
我的 printf.exe 应用程序是一个 Windows 控制台程序,它是众所周知的 printf CRT 函数的包装器,从而允许文本和格式化数值显示在 cmd.exe 窗口中。此外,我的程序 printf.exe 还允许使用基于堆栈的 Hewlett-Packard 的相同方法和功能对 32 位整数和 64 位 double 浮点数执行 Reverse Polish Notation 算术运算计算器.
新的 printf.exe 版本 2.11 还管理字符串操作,并允许使用最简单的编程方案编写脚本(程序)。下面的批处理文件包含一个 printf.exe 程序,也可以解决此问题。请注意,printf.exe 方法使用与上一个批处理文件相同的公式。
@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! :) */
输出:
The last day of the previous month was: 2023/07/31 (Monday)
虽然看起来很复杂,但 printf.exe 指令非常简单
。 HP计算器用户在了解差异和编程方案后,可以在几分钟内开始编写printf.exe程序。您可以在 Base64 解码器中查看 printf.exe 程序的更大示例 您可以从
此链接