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

前端 未结 28 2208
别跟我提以往
别跟我提以往 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条回答
  •  猫巷女王i
    2020-11-21 05:59

    The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in Windows XP Professional and higher.

    @echo off
    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
    set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
    set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
    
    set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%%Min%-%Sec%"
    echo datestamp: "%datestamp%"
    echo timestamp: "%timestamp%"
    echo fullstamp: "%fullstamp%"
    pause
    

提交回复
热议问题