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

前端 未结 28 2076
别跟我提以往
别跟我提以往 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: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
    
    0 讨论(0)
  • 2020-11-21 05:59

    Just use this line:

    PowerShell -Command "get-date"
    
    0 讨论(0)
  • 2020-11-21 05:59
    :: GetDate.cmd -> Uses WMIC.exe to get current date and time in ISO 8601 format
    :: - Sets environment variables %_isotime% and %_now% to current time
    :: - On failure, clears these environment variables
    :: Inspired on -> https://ss64.com/nt/syntax-getdate.html
    :: - (cX) 2017 adolfo.dimare@gmail.com
    :: - http://stackoverflow.com/questions/203090
    @echo off
    
    set _isotime=
    set _now=
    
    :: Check that WMIC.exe is available
    WMIC.exe Alias /? >NUL 2>&1 || goto _WMIC_MISSING_
    
    if not (%1)==() goto _help
    SetLocal EnableDelayedExpansion
    
    :: Use WMIC.exe to retrieve date and time
    FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC.exe Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
        IF "%%~L"=="" goto  _WMIC_done_
            set _yyyy=%%L
            set _mm=00%%J
            set _dd=00%%G
            set _hour=00%%H
            set _minute=00%%I
            set _second=00%%K
    )
    :_WMIC_done_
    
    ::   1    2     3       4      5      6
    :: %%G  %%H    %%I     %%J    %%K    %%L
    :: Day  Hour  Minute  Month  Second  Year
    :: 27   9     35      4      38      2017
    
    :: Remove excess leading zeroes
            set _mm=%_mm:~-2%
            set _dd=%_dd:~-2%
            set _hour=%_hour:~-2%
            set _minute=%_minute:~-2%
            set _second=%_second:~-2%
    :: Syntax -> %variable:~num_chars_to_skip,num_chars_to_keep%
    
    :: Set date/time in ISO 8601 format:
            Set _isotime=%_yyyy%-%_mm%-%_dd%T%_hour%:%_minute%:%_second%
    :: -> http://google.com/search?num=100&q=ISO+8601+format
    
    if 1%_hour% LSS 112 set _now=%_isotime:~0,10% %_hour%:%_minute%:%_second%am
    if 1%_hour% LSS 112 goto _skip_12_
        set /a _hour=1%_hour%-12
        set _hour=%_hour:~-2%
        set _now=%_isotime:~0,10% %_hour%:%_minute%:%_second%pm
        :: -> https://ss64.com/nt/if.html
        :: -> http://google.com/search?num=100&q=SetLocal+EndLocal+Windows
        :: 'if () else ()' will NOT set %_now% correctly !?
    :_skip_12_
    
    EndLocal & set _isotime=%_isotime% & set _now=%_now%
    goto _out
    
    :_WMIC_MISSING_
    echo.
    echo WMIC.exe command not available
    echo - WMIC.exe needs Administrator privileges to run in Windows
    echo - Usually the path to WMIC.exe is "%windir%\System32\wbem\WMIC.exe"
    
    :_help
    echo.
    echo GetDate.cmd: Uses WMIC.exe to get current date and time in ISO 8601 format
    echo.
    echo    %%_now%%     environment variable set to current date and time
    echo    %%_isotime%% environment variable to current time in ISO format
    echo    set _today=%%_isotime:~0,10%%
    echo.
    
    :_out
    :: EOF: GetDate.cmd
    
    0 讨论(0)
  • 2020-11-21 06:00

    Short answer :

     :: Start - Run , type:
     cmd /c "powershell get-date -format ^"{yyyy-MM-dd HH:mm:ss}^"|clip"
    
     :: click into target media, Ctrl + V to paste the result 
    

    Long answer

        @echo off
        :: START USAGE  ==================================================================
        ::SET THE NICETIME 
        :: SET NICETIME=BOO
        :: CALL GetNiceTime.cmd 
    
        :: ECHO NICETIME IS %NICETIME%
    
        :: echo nice time is %NICETIME%
        :: END USAGE  ==================================================================
    
        echo set hhmmsss
        :: this is Regional settings dependant so tweak this according your current settings
        for /f "tokens=1-3 delims=:" %%a in ('echo %time%') do set hhmmsss=%%a%%b%%c 
        ::DEBUG ECHO hhmmsss IS %hhmmsss%
        ::DEBUG PAUSE
        echo %yyyymmdd%
            :: this is Regional settings dependant so tweak this according your current settings
        for /f "tokens=1-3 delims=." %%D in ('echo %DATE%') do set  yyyymmdd=%%F%%E%%D
        ::DEBUG ECHO yyyymmdd IS %yyyymmdd%
        ::DEBUG PAUSE
    
    
        set NICETIME=%yyyymmdd%_%hhmmsss%
        ::DEBUG echo THE NICETIME IS %NICETIME%
    
        ::DEBUG PAUSE
    
    0 讨论(0)
  • 2020-11-21 06:00

    And here is a similar batch-file for the time portion.

    :: http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-usi
    :: Works on any NT/2k machine independent of regional time settings
    ::
    :: Gets the time in ISO 8601 24-hour format
    ::
    :: Note that %time% gets you fractions of seconds, and time /t doesn't, but gets you AM/PM if your locale supports that.
    :: Since ISO 8601 does not care about AM/PM, we use %time%
    ::
        @ECHO off
        SETLOCAL ENABLEEXTENSIONS
        for /f "tokens=1-4 delims=:,.-/ " %%i in ('echo %time%') do (
          set 'hh'=%%i
          set 'mm'=%%j
          set 'ss'=%%k
          set 'ff'=%%l)
        ENDLOCAL & SET v_Hour=%'hh'%& SET v_Minute=%'mm'%& SET v_Second=%'ss'%& SET v_Fraction=%'ff'%
    
        ECHO Now is Hour: [%V_Hour%] Minute: [%V_Minute%] Second: [%v_Second%] Fraction: [%v_Fraction%]
        set timestring=%V_Hour%%V_Minute%%v_Second%.%v_Fraction%
        echo %timestring%
    
        :EOF
    

    --jeroen

    0 讨论(0)
  • 2020-11-21 06:00

    I had a similar problem. I have an automatic daily download from an FTP server of an encrypted file. I wanted to decrypt the file using gpg, rename the file to the current date (YYYYMMDD format) and drop the decrypted file into a folder for the correct department.

    I went through several suggestions for renaming the file according to date and was having no luck until I stumbled upon this simple solution.

    for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "decrypted.txt" %%g-%%e-%%f.txt
    

    It worked perfectly (i.e., the filename comes out as "2011-06-14.txt").

    (Source)

    0 讨论(0)
提交回复
热议问题