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

前端 未结 14 1370
长情又很酷
长情又很酷 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:26

    You can use simple variable syntax, here is an example:

    @echo off
    set month=%date:~0,2%
    set day=%date:~3,2%
    set year=%date:~6,4%
    echo The current month is %month%
    echo The current day is %day%
    echo The current year is %year%
    pause >nul
    

    Another option is the for command, again here is my example:

    @echo off
    for /f "delims=/ tokens=1-3" %%a in ("%date%") do (
    set month=%%a
    set day=%%b
    set year=%%c
    )
    echo The current month is %month%
    echo The current day is %day%
    echo The current year is %year%
    pause >nul
    

提交回复
热议问题