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

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

    Unfortunately this is not immune to regional settings, but it does what you want.

    set hour=%time:~0,2%
    if "%time:~0,1%"==" " set hour=0%time:~1,1%
    set _my_datetime=%date:~10,4%-%date:~4,2%-%date:~7,2%_%hour%%time:~3,2%
    

    Amazing the stuff you can find on Wikipedia.

    0 讨论(0)
  • 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

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

    Here's a way to get date time in a single line:

    for /f "tokens=2,3,4,5,6 usebackq delims=:/ " %a in ('%date% %time%') do echo %c-%a-%b %d%e
    

    In the US this will output "yyyy-mm-dd hhmm". Different regional settings will result in different %date% outputs, but you can modify the token order.

    If you want a different format, modify the echo statement by rearranging the tokens or using different (or no) separators.

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

    I know that there are numerous ways mentioned already. But here is my way to break it down to understand how it is done. Hopefully, it is helpful for someone who like step by step method.

    :: Check your local date format
    echo %date%
    
        :: Output is Mon 08/15/2016
    
    :: get day (start index, number of characters)
    ::         (index starts with zero)
    set myday=%DATE:~0,4%
    echo %myday%
        :: output is Mon 
    
    :: get month
    set mymonth=%DATE:~4,2%
    echo %mymonth%
        :: output is 08
    
    :: get date 
    set mydate=%DATE:~7,2% 
    echo %mydate%
        :: output is 15
    
    :: get year
    set myyear=%DATE:~10,4%
    echo %myyear%
        :: output is 2016
    
    0 讨论(0)
  • 2020-11-21 05:58

    Matthew Johnson's one-liner solution to get the one-liner date and time is eloquent and useful.

    It does however need a simple modification to work from within a batch file:

    for /f "tokens=2,3,4,5,6 usebackq delims=:/ " %%a in ('%date% %time%') do echo %%c-%%a-%%b %%d%%e
    
    0 讨论(0)
  • 2020-11-21 05:58

    Combine Powershell into a batch file and use the meta variables to assign each:

    @echo off
    for /f "tokens=1-6 delims=-" %%a in ('PowerShell -Command "& {Get-Date -format "yyyy-MM-dd-HH-mm-ss"}"') do (
        echo year: %%a
        echo month: %%b
        echo day: %%c
        echo hour: %%d
        echo minute: %%e
        echo second: %%f
    )
    

    You can also change the the format if you prefer name of the month MMM or MMMM and 12 hour to 24 hour formats hh or HH

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