How do I get the day month and year from a Windows cmd.exe script?

前端 未结 14 1354
长情又很酷
长情又很酷 2020-12-23 17:02

How do I get the current day month and year from inside a Windows cmd script? I need to get each value into a separate variable.

相关标签:
14条回答
  • 2020-12-23 17:04

    For one line!


    Try using for wmic OS Get localdatetime^|find "." in for /f without tokens and/or delims, this works in any language / region and also, no user settings interfere with the layout of the output.

    • In command line:
    for /f %i in ('wmic OS Get localdatetime^|find "."')do @cmd/v/c "set _date=%i &echo= year: !_date:~0,4!&&echo=month:   !_date:~4,2!&echo=  day:   !_date:~6,2!"
    

    • In bat/cmd file:
    for /f %%i in ('wmic OS Get localdatetime^|find "."')do @cmd/v/c "set _date=%%i &echo= year: !_date:~0,4!&&echo=month:   !_date:~4,2!&echo=  day:   !_date:~6,2!"
    

    Results:


     year: 2019
    month:   06
      day:   12
    

    • With Hour and Minute in bat/cmd file:
    for /f %%i in ('wmic OS Get localdatetime^|find "."')do @cmd/v/c "set _date=%%i &echo=  year: !_date:~0,4!&&echo= month:   !_date:~4,2!&echo=   day:   !_date:~6,2!&echo=  hour:   !_date:~8,2!&echo=minute:   !_date:~10,2!"
    

    • With Hour and Minute in command line:
    for /f %i in ('wmic OS Get localdatetime^|find "."')do @cmd/v/c "set _date=%i &echo=  year: !_date:~0,4!&&echo= month:   !_date:~4,2!&echo=   day:   !_date:~6,2!&echo=  hour:   !_date:~8,2!&echo=minute:   !_date:~10,2!"
    

    Results:


      year: 2020
     month:   05
       day:   16
      hour:   00
    minute:   46
    
    0 讨论(0)
  • 2020-12-23 17:06

    To get the year, month, and day you can use the %date% environment variable and the :~ operator. %date% expands to something like Thu 08/12/2010 and :~ allows you to pick up specific characters out of a variable:

    set year=%date:~10,4%
    set month=%date:~4,2%
    set day=%date:~7,2%
    set filename=%year%_%month%_%day%
    

    Use %time% in similar fashion to get what you need from the current time.

    set /? will give you more information on using special operators with variables.

    0 讨论(0)
  • 2020-12-23 17:08
    echo %Date:~7,2% gets current day
    

    7 is starting position 2 number of digits to display

    echo %Date:~7,2% gets current day
    
    echo %Date:~4,2% gets current month
    
    echo %Date:~10,4% gets current year
    
    0 讨论(0)
  • 2020-12-23 17:09

    The following batch code returns the components of the current date in a locale-independent manner and stores day, month and year in the variables CurrDay, CurrMonth and CurrYear, respectively:

    for /F "skip=1 delims=" %%F in ('
        wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE
    ') do (
        for /F "tokens=1-3" %%L in ("%%F") do (
            set CurrDay=0%%L
            set CurrMonth=0%%M
            set CurrYear=%%N
        )
    )
    set CurrDay=%CurrDay:~-2%
    set CurrMonth=%CurrMonth:~-2%
    echo Current day  :  %CurrDay%
    echo Current month:  %CurrMonth%
    echo Current year :%CurrYear%
    

    There are two nested for /F loops to work around an issue with the wmic command, whose output is in unicode format; using a single loop results in additional carriage-return characters which impacts proper variable expansion.

    Since day and month may also consist of a single digit only, I prepended a leading zero 0 in the loop construct. Afterwards, the values are trimmed to always consist of two digits.

    0 讨论(0)
  • 2020-12-23 17:10

    A variant of script that works locale-independently. Put it in a text file with .cmd extension and run.

    ::: Begin set date
    
    for /f "tokens=1-4 delims=/-. " %%i in ('date /t') do (call :set_date %%i %%j %%k %%l)
    goto :end_set_date
    
    :set_date
    if "%1:~0,1%" gtr "9" shift
    for /f "skip=1 tokens=2-4 delims=(-)" %%m in ('echo,^|date') do (set %%m=%1&set %%n=%2&set %%o=%3)
    goto :eof
    
    :end_set_date
    ::: End set date
    
    echo day in 'DD' format is %dd%; month in 'MM' format is %mm%; year in 'YYYY' format is %yy%
    

    The variables %dd%, %mm% and %yy% will keep the day('DD' format), the month('MM' format) and the year('YYYY' format) respectively.

    0 讨论(0)
  • 2020-12-23 17:10

    This variant works for all localizations:

    @echo off
    FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
        if "%%B" NEQ "" (
            SET /A FDATE=%%F*10000+%%D*100+%%A
        )
    )
    @echo on
    echo date=%FDATE%
    echo year=%FDATE:~2,2%
    echo month=%FDATE:~4,2%
    
    0 讨论(0)
提交回复
热议问题