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

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

    This isn't really briefer but might be a more flexible way (credit):

    FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
    FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
    FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
    FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
    SET date=%mm%%dd%%yyyy%
    
    0 讨论(0)
  • 2020-11-21 05:44

    Regional independent solution generating the ISO date format:

    rem save the existing format definition
    for /f "skip=2 tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate') do set FORMAT=%%a
    rem set ISO specific format definition
    reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /f /d yyyy-MM-dd 1>nul:
    rem query the date in the ISO specific format 
    set ISODATE=%DATE%
    rem restore previous format definition
    reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /f /d %FORMAT% 1>nul:
    

    What could still be optimized: Other processes might get confused if using the date format in the short period while it is modified. So parsing the output according to the existing format string could be 'safer' - but will be more complicated

    0 讨论(0)
  • 2020-11-21 05:44

    With Windows 7, this code works for me:

    SET DATE=%date%
    SET YEAR=%DATE:~0,4%
    SET MONTH=%DATE:~5,2%
    SET DAY=%DATE:~8,2%
    ECHO %YEAR%
    ECHO %MONTH%
    ECHO %DAY%
    
    SET DATE_FRM=%YEAR%-%MONTH%-%DAY% 
    ECHO %DATE_FRM%
    
    0 讨论(0)
  • 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:

    0 讨论(0)
  • 2020-11-21 05:47

    Regionally independent date time parsing

    The output format of %DATE% and of the dir command is regionally dependent and thus neither robust nor smart. date.exe (part of UnxUtils) delivers any date and time information in any thinkable format. You may also extract the date/time information from any file with date.exe.

    Examples: (in a cmd-script use %% instead of %)

    date.exe +"%Y-%m-%d"
    2009-12-22

    date.exe +"%T"
    18:55:03

    date.exe +"%Y%m%d %H%M%S: Any text"
    20091222 185503: Any text

    date.exe +"Text: %y/%m/%d-any text-%H.%M"
    Text: 09/12/22-any text-18.55

    Command: date.exe +"%m-%d """%H %M %S """"
    07-22 "18:55:03"`

    The date/time information from a reference file:
    date.exe -r c:\file.txt +"The timestamp of file.txt is: %Y-%m-%d %H:%M:%S"

    Using it in a CMD script to get year, month, day, time information:

    for /f "tokens=1,2,3,4,5,6* delims=," %%i in ('C:\Tools\etc\date.exe +"%%y,%%m,%%d,%%H,%%M,%%S"') do set yy=%%i& set mo=%%j& set dd=%%k& set hh=%%l& set mm=%%m& set ss=%%n
    

    Using it in a CMD script to get a timestamp in any required format:

    for /f "tokens=*" %%i in ('C:\Tools\etc\date.exe +"%%y-%%m-%%d %%H:%%M:%%S"') do set timestamp=%%i
    

    Extracting the date/time information from any reference file.

    for /f "tokens=1,2,3,4,5,6* delims=," %%i in ('C:\Tools\etc\date.exe -r file.txt +"%%y,%%m,%%d,%%H,%%M,%%S"') do set yy=%%i& set mo=%%j& set dd=%%k& set hh=%%l& set mm=%%m& set ss=%%n
    

    Adding to a file its date/time information:

    for /f "tokens=*" %%i in ('C:\Tools\etc\date.exe -r file.txt +"%%y-%%m-%%d.%%H%%M%%S"') do ren file.txt file.%%i.txt
    

    date.exe is part of the free GNU tools which need no installation.

    NOTE: Copying date.exe into any directory which is in the search path may cause other scripts to fail that use the Windows built-in date command.

    0 讨论(0)
  • 2020-11-21 05:47

    I use this (again not region independent (UK))

    set bklog=%date:~6,4%-%date:~3,2%-%date:~0,2%_%time:~0,2%%time:~3,2%
    
    0 讨论(0)
提交回复
热议问题