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

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

    I changed the answer with the batch file from vMax so it works with the Dutch language too.
    The Dutch - persistent as we are - have a few changes in the %date%, date/t, and date that break the original batch-file.

    It would be nice if some people can check this against other Windows locales as well, and report back the results.
    If the batch-file fails at your location, then please include the output of these two statements on the command prompt:
    echo:^|date
    date/t

    This is a sample of the output you should get from the batch-file:

    C:\temp>set-date-cmd.bat
    Today is Year: [2011] Month: [01] Day: [03]
    20110103
    

    Here is the revised code with comments on why:

    :: https://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 date settings
    ::
    :: 20110103 - adapted by jeroen@pluimers.com for Dutch locale
    :: Dutch will get jj as year from echo:^|date, so the '%%c' trick does not work as it will fill 'jj', but we want 'yy'
    :: luckily, all countries seem to have year at the end: http://en.wikipedia.org/wiki/Calendar_date
    ::            set '%%c'=%%k
    ::            set 'yy'=%%k
    ::
    :: In addition, date will display the current date before the input prompt using dashes
    :: in Dutch, but using slashes in English, so there will be two occurances of the outer loop in Dutch
    :: and one occurence in English.
    :: This skips the first iteration:
    ::        if "%%a" GEQ "A"
    ::
    :: echo:^|date
    :: Huidige datum: ma 03-01-2011
    :: Voer de nieuwe datum in: (dd-mm-jj)
    :: The current date is: Mon 01/03/2011
    :: Enter the new date: (mm-dd-yy)
    ::
    :: date/t
    :: ma 03-01-2011
    :: Mon 01/03/2011
    ::
    :: The assumption in this batch-file is that echo:^|date will return the date format
    :: using either mm and dd or dd and mm in the first two valid tokens on the second line, and the year as the last token.
    ::
    :: The outer loop will get the right tokens, the inner loop assigns the variables depending on the tokens.
    :: That will resolve the order of the tokens.
    ::
    @ECHO off
        set v_day=
        set v_month=
        set v_year=
    
        SETLOCAL ENABLEEXTENSIONS
        if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
    ::DEBUG echo toks=%toks%
          for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
    ::DEBUG echo first token=%%a
            if "%%a" GEQ "A" (
              for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
                set '%%a'=%%i
                set '%%b'=%%j
                set 'yy'=%%k
              )
            )
          )
          if %'yy'% LSS 100 set 'yy'=20%'yy'%
          set Today=%'yy'%-%'mm'%-%'dd'%
    
        ENDLOCAL & SET v_year=%'yy'%& SET v_month=%'mm'%& SET v_day=%'dd'%
    
        ECHO Today is Year: [%V_Year%] Month: [%V_Month%] Day: [%V_Day%]
        set datestring=%V_Year%%V_Month%%V_Day%
        echo %datestring%
    
        :EOF
    

    --jeroen

提交回复
热议问题