How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?

前端 未结 28 2094
别跟我提以往
别跟我提以往 2020-11-21 05:28

Update: Now that it\'s 2016 I\'d use PowerShell for this unless there\'s a really compelling backwards-compatible reason for it, particularly because of the regional setting

28条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 05:58

    Combine Powershell into a batch file and use the meta variables to assign each:

    @echo off
    for /f "tokens=1-6 delims=-" %%a in ('PowerShell -Command "& {Get-Date -format "yyyy-MM-dd-HH-mm-ss"}"') do (
        echo year: %%a
        echo month: %%b
        echo day: %%c
        echo hour: %%d
        echo minute: %%e
        echo second: %%f
    )
    

    You can also change the the format if you prefer name of the month MMM or MMMM and 12 hour to 24 hour formats hh or HH

提交回复
热议问题