批处理 编写获得前一天的日期的脚本
时间: 2023-11-30 21:04:47 浏览: 134
在 Windows 环境下,可以使用以下批处理脚本来获取前一天的日期:
```
@echo off
setlocal enabledelayedexpansion
set "today=%%date%%"
set "day=!today:~0,2!"
set "month=!today:~3,2!"
set "year=!today:~6,4!"
set /a "day-=1"
if !day! equ 0 (
set /a "month-=1"
if !month! equ 0 (
set /a "year-=1"
set "month=12"
)
if !month! equ 4 echo 30
if !month! equ 6 echo 30
if !month! equ 9 echo 30
if !month! equ 11 echo 30
if !month! equ 2 (
if !year! equ 1900 (
set /a "day=28"
) else (
set /a "leap=year %% 4"
if !leap! equ 0 (
set /a "leap=year %% 100"
if !leap! equ 0 (
set /a "leap=year %% 400"
if !leap! equ 0 (
set /a "day=29"
) else (
set /a "day=28"
)
) else (
set /a "day=29"
)
) else (
set /a "day=28"
)
)
) else (
set /a "day=31"
)
)
if !day! lss 10 set "day=0!day!"
if !month! lss 10 set "month=0!month!"
set "yesterday=!year!!month!!day!"
echo Yesterday's date: !yesterday!
```
这个脚本首先获取当前日期,并将其拆分为日、月和年三个部分。然后,它减去一天,并根据月份调整日期(例如,如果减去一天后日期变成了 0,则将月份减去一,然后根据月份确定正确的日期)。最后,将新日期格式化为一个字符串,并将其存储在变量 `yesterday` 中。
阅读全文