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

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

    A function that is based on wmic:

    :Now  -- Gets the current date and time into separate variables
    ::    %~1: [out] Year
    ::    %~2: [out] Month
    ::    %~3: [out] Day
    ::    %~4: [out] Hour
    ::    %~5: [out] Minute
    ::    %~6: [out] Second
      setlocal
      for /f %%t in ('wmic os get LocalDateTime ^| findstr /b [0-9]') do set T=%%t
      endlocal & (
        if "%~1" neq "" set %~1=%T:~0,4%
        if "%~2" neq "" set %~2=%T:~4,2%
        if "%~3" neq "" set %~3=%T:~6,2%
        if "%~4" neq "" set %~4=%T:~8,2%
        if "%~5" neq "" set %~5=%T:~10,2%
        if "%~6" neq "" set %~6=%T:~12,2%
      )
    goto:eof
    

    Upside: Region independent. Downside: Only system administrators can run wmic.exe.

    Usage:

    call:Now Y M D H N S
    echo %Y%-%M%-%D% %H%:%N%:%S%
    

    This echos a string like this:

    2014-01-22 12:51:53
    

    Note that function parameters are out-Parameters - that is, you must supply variable names instead of values.

    All parameters are optional, so call:Now Y M is a valid call if you only want to get year and month.

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

    Here's a variant from alt.msdos.batch.nt that works local-independently.

    Put this in a text file, e.g. getDate.cmd

    -----------8<------8<------------ snip -- snip ----------8<-------------
        :: Works on any NT/2k machine independent of regional date settings
        @ECHO off
        SETLOCAL ENABLEEXTENSIONS
        if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
        for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
          for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
            set '%%a'=%%i
            set '%%b'=%%j
            set '%%c'=%%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%]
    
        :EOF
    -----------8<------8<------------ snip -- snip ----------8<-------------
    

    To get the code to work sans error msg's to stderr, I had to add the single quotes arount the variable assignments for %%a, %%b and %%c. My locale (PT) was causing errors at one stage in the looping/parsing where stuff like "set =20" was getting executed. The quotes yield a token (albeit empty) for the left-hand side of the assignment statement.

    The downside is the messy locale variable names: 'yy', 'mm' and 'dd'. But hey, who cares!

    0 讨论(0)
  • 2020-11-21 06:03
    "d:\Program Files\7-Zip\7z.exe" a -r code_%date:~10,4%-%date:~4,2%-%date:~7,2%.zip
    
    0 讨论(0)
  • 2020-11-21 06:03

    http://sourceforge.net/projects/unxutils/files/

    Look inside the ZIP file for something called "Date.exe" and rename it "DateFormat.exe" (to avoid conflicts).

    Put it in your Windows system32 folder.

    It has a lot of "date output" options.

    For help, use DateFormat.exe --h

    I'm not sure how you would put its output into an environment variable... using SET.

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