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

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

提交回复
热议问题