How do I get the day month and year from a Windows cmd.exe script?

前端 未结 14 1394
长情又很酷
长情又很酷 2020-12-23 17:02

How do I get the current day month and year from inside a Windows cmd script? I need to get each value into a separate variable.

14条回答
  •  孤城傲影
    2020-12-23 17:10

    A variant of script that works locale-independently. Put it in a text file with .cmd extension and run.

    ::: Begin set date
    
    for /f "tokens=1-4 delims=/-. " %%i in ('date /t') do (call :set_date %%i %%j %%k %%l)
    goto :end_set_date
    
    :set_date
    if "%1:~0,1%" gtr "9" shift
    for /f "skip=1 tokens=2-4 delims=(-)" %%m in ('echo,^|date') do (set %%m=%1&set %%n=%2&set %%o=%3)
    goto :eof
    
    :end_set_date
    ::: End set date
    
    echo day in 'DD' format is %dd%; month in 'MM' format is %mm%; year in 'YYYY' format is %yy%
    

    The variables %dd%, %mm% and %yy% will keep the day('DD' format), the month('MM' format) and the year('YYYY' format) respectively.

提交回复
热议问题