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

前端 未结 28 2746
别跟我提以往
别跟我提以往 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:45

    Given a known locality, for reference in functional form. The ECHOTIMESTAMP call shows how to get the timestamp into a variable (DTS in this example.)

    @ECHO off
    
    CALL :ECHOTIMESTAMP
    GOTO END
    
    :TIMESTAMP
    SETLOCAL  EnableDelayedExpansion
        SET DATESTAMP=!DATE:~10,4!-!DATE:~4,2!-!DATE:~7,2!
        SET TIMESTAMP=!TIME:~0,2!-!TIME:~3,2!-!TIME:~6,2!
        SET DTS=!DATESTAMP: =0!-!TIMESTAMP: =0!
    ENDLOCAL & SET "%~1=%DTS%"
    GOTO :EOF
    
    :ECHOTIMESTAMP
    SETLOCAL
        CALL :TIMESTAMP DTS
        ECHO %DTS%
    ENDLOCAL
    GOTO :EOF
    
    :END
    
    EXIT /b 0
    

    And saved to file, timestamp.bat, here's the output:

提交回复
热议问题