windows 判断输入日期格式

内容纲要
@echo off&setlocal enabledelayedexpansion
:start
echo "请按正确的日期格式输入要同步的数据日期,例如:20220308"
set /p p_date=

if not defined p_date goto start

for /f "delims=0123456789" %%i in ("%p_date%") do if not "%%a"=="" echo "日期格式不正确,请重新输入"&goto start

if %p_date% lss 20010101 (
    echo "日期格式不正确,请重新输入"
    goto start
)

if %p_date% gtr 20990101 (
    echo "日期格式不正确,请重新输入"
    goto start
)

set "date_y=%p_date:~0,4%"

set "date_m=%p_date:~4,2%"

set "date_d=%p_date:~6,2%"

if %date_m% gtr 12 (
    echo "日期格式不正确,请重新输入" 
    goto start
)
if %date_m% lss 1 (
    echo "日期格式不正确,请重新输入"
    goto start
)

if %date_d% gtr 31 (
    echo "日期格式不正确,请重新输入"
    goto start
)
if %date_d% lss 1 (
    echo "日期格式不正确,请重新输入"
    goto start
)

echo %date_y%-%date_m%-%date_d%

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注