bat脚本计算前一天的日期
时间: 2023-04-03 08:03:58 浏览: 205
可以使用以下代码计算前一天的日期:
```
@echo off
setlocal enabledelayedexpansion
set "today=%date%"
set /a "yesterday=1%today:~0,2%-10000"
set /a "yesterday=!yesterday! * 100 + 1%today:~3,2%-100"
set /a "yesterday=!yesterday! * 100 + 1%today:~6,2%-100"
set /a "yesterday-=1"
echo %yesterday%
```
这段代码会获取当前日期并计算前一天的日期,最后输出前一天的日期。
相关问题
用bat脚本计算前一天时间
可以使用以下代码计算前一天的日期:
```
@echo off
setlocal enabledelayedexpansion
set "today=%date%"
set /a "day=1%today:~0,2%-100"
set /a "month=1%today:~3,2%-100"
set /a "year=today:~6,4%"
set /a "days=0"
if %month% equ 1 (
set /a "days=31"
) else if %month% equ 2 (
if %year% equ 0 (
set /a "days=29"
) else if %year% mod 4 equ 0 (
if %year% mod 100 neq 0 (
set /a "days=29"
) else if %year% mod 400 eq 0 (
set /a "days=29"
) else (
set /a "days=28"
)
) else (
set /a "days=28"
)
) else if %month% equ 3 (
set /a "days=31"
) else if %month% equ 4 (
set /a "days=30"
) else if %month% equ 5 (
set /a "days=31"
) else if %month% equ 6 (
set /a "days=30"
) else if %month% equ 7 (
set /a "days=31"
) else if %month% equ 8 (
set /a "days=31"
) else if %month% equ 9 (
set /a "days=30"
) else if %month% equ 10 (
set /a "days=31"
) else if %month% equ 11 (
set /a "days=30"
) else if %month% equ 12 (
set /a "days=31"
)
set /a "day=day-1"
if %day% equ 0 (
set /a "month=month-1"
if %month% equ 0 (
set /a "year=year-1"
set /a "month=12"
)
if %month% equ 2 (
if %year% equ 0 (
set /a "days=29"
) else if %year% mod 4 equ 0 (
if %year% mod 100 neq 0 (
set /a "days=29"
) else if %year% mod 400 eq 0 (
set /a "days=29"
) else (
set /a "days=28"
)
) else (
set /a "days=28"
)
)
set /a "day=days"
)
set "month=0%month%"
set "day=0%day%"
set "year=000%year%"
set "month=!month:~-2!"
set "day=!day:~-2!"
set "year=!year:~-4!"
echo %year%%month%%day%
```
这个脚本会获取当前日期,然后计算前一天的日期,并将其输出。注意,这个脚本只能在 Windows 系统上运行。
bat脚本获取前一天的日期
可以使用以下代码获取前一天的日期:
```
@echo off
setlocal enabledelayedexpansion
set "today=%date%"
set /a "day=1"
set "year=!today:~0,4!"
set "month=!today:~5,2!"
set "day=!today:~8,2!"
if %day% leq %day% set /a "month-=1"
if !month! equ 0 set /a "year-=1" & set "month=12"
if !month! equ 2 (
if !year! equ 1900 (
set /a "day=28"
) else if !year! equ 1800 (
set /a "day=28"
) else if !year! equ 1700 (
set /a "day=28"
) else if !year! equ 1600 (
set /a "day=29"
) else if !year! equ 1500 (
set /a "day=28"
) else if !year! equ 1400 (
set /a "day=28"
) else if !year! equ 1300 (
set /a "day=28"
) else if !year! equ 1200 (
set /a "day=29"
) else if !year! equ 1100 (
set /a "day=28"
) else if !year! equ 1000 (
set /a "day=28"
) else if !year! equ 900 (
set /a "day=28"
) else if !year! equ 800 (
set /a "day=29"
) else if !year! equ 700 (
set /a "day=28"
) else if !year! equ 600 (
set /a "day=28"
) else if !year! equ 500 (
set /a "day=28"
) else if !year! equ 400 (
set /a "day=29"
) else if !year! equ 300 (
set /a "day=28"
) else if !year! equ 200 (
set /a "day=28"
) else if !year! equ 100 (
set /a "day=28"
) else if !year! equ 0 (
set /a "day=28"
) else (
set /a "day=29"
)
) else if !month! equ 4 (
set /a "day=30"
) else if !month! equ 6 (
set /a "day=30"
) else if !month! equ 9 (
set /a "day=30"
) else if !month! equ 11 (
set /a "day=30"
) else (
set /a "day=31"
)
if !month! lss 10 set "month=0!month!"
if !day! lss 10 set "day=0!day!"
set "yesterday=!year!!month!!day!"
echo %yesterday%
```
这段代码会获取当前日期,然后计算前一天的日期,并输出前一天的日期。
阅读全文