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

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

    The only reliably way I know is to use VBScript to do the heavy work for you. There is no portable way of getting the current date in a usable format with a batch file alone. The following VBScript file

    Wscript.Echo("set Year=" & DatePart("yyyy", Date))
    Wscript.Echo("set Month=" & DatePart("m", Date))
    Wscript.Echo("set Day=" & DatePart("d", Date))
    

    and this batch snippet

    for /f "delims=" %%x in ('cscript /nologo date.vbs') do %%x
    echo %Year%-%Month%-%Day%
    

    should work, though.

    While you can get the current date in a batch file with either date /t or the %date% pseudo-variable, both follow the current locale in what they display. Which means you get the date in potentially any format and you have no way of parsing that.

提交回复
热议问题