本帖最后由 yakeyun 于 2024-6-5 08:55 编辑
请教一下各位大佬有没有好的办法完善一下下面代码:- @echo off&setlocal enabledelayedexpansion
-
- for /f "tokens=1-3 delims=/-" %%a in ("%date%") do (
- set "year=%%a"
- set "month=%%b"
- set "day=%%c"
- )
-
- rem 显示当前日期
- echo 当前日期: %year%年%month%月%day%日
- echo,
-
- rem 推算前7天的日期并显示
- set adN=0
- for /L %%i in (1, 1, 7) do (
- set /a adN+=1
- set /a "prev_day=%day%-%%i"
- set "prev_month=!month!"
- set "prev_year=!year!"
- if !prev_day! equ 0 (set "prev_day=31" & set /a "prev_month=!month!-1")
- if !prev_day! equ -1 (set "prev_day=30" & set /a "prev_month=!month!-1")
- if !prev_day! equ -2 (set "prev_day=29" & set /a "prev_month=!month!-1")
- if !prev_day! equ -3 (set "prev_day=28" & set /a "prev_month=!month!-1")
- if !prev_day! equ -4 (set "prev_day=27" & set /a "prev_month=!month!-1")
- if !prev_day! equ -5 (set "prev_day=26" & set /a "prev_month=!month!-1")
- if !prev_day! equ -6 (set "prev_day=25" & set /a "prev_month=!month!-1")
- if !prev_day! geq 1 (if !prev_day! lss 10 (
- set /a "prev_day=!prev_day!
- set "prev_day=0!prev_day!"
- ))
- if !prev_month! geq 1 (if !prev_month! lss 10 (
- set /a "prev_month=!prev_month!"
- set "prev_month=0!prev_month!"
- ))
- if !month! equ 1 (if !day! equ 1 (set /a prev_month=12 & set /a prev_year=!year! - 1))
-
- echo 前!adN!天日期为: !prev_year!年!prev_month!月!prev_day!日
-
- )
- pause>nul 2>nul
复制代码 上面为基础代码,获取当前系统的年,月,日。然后推演前7天的年月日,并输出显示出来。
已知问题如下:
1、无法自动判断当前年份有31还是30天,或者无法自动判断2月有28天还是29天。
2、遇到与8日期相关的数值,无法自动执行,系统会因为进制原因而无法处理。 |