How to get date in BAT file

后端 未结 5 2005
醉酒成梦
醉酒成梦 2020-12-05 04:36

I need to get today date in Window *.bat file. After it I would like to get day, month and year. How can I do this?
I can\'t use PowerShell

相关标签:
5条回答
  • 2020-12-05 05:02
    set datestr=%date%
    set result=%datestr:/=-%
    @echo %result%
    pause
    
    0 讨论(0)
  • 2020-12-05 05:08

    Locale-independent one liner to get any date format you like. I use it to generate archive names. Back quote option is needed because PowerShell command line is using single quotes.

    :: Get date in yyyyMMdd_HHmm format to use with file name.
    FOR /f "usebackq" %%i IN (`PowerShell ^(Get-Date^).ToString^('yyyy-MM-dd'^)`) DO SET DTime=%%i
    
    :: Get formatted yesterday date.
    FOR /f "usebackq" %%i IN (`PowerShell ^(Get-Date^).AddDays^(-1^).ToString^('yyyy-MM-dd'^)`) DO SET DTime=%%i
    
    :: Show file name with the date.
    echo Archive.%DTime%.zip
    
    0 讨论(0)
  • 2020-12-05 05:10

    %date% will give you the date.

    %time% will give you the time.

    The date and time /t commands may give you more detail.

    0 讨论(0)
  • 2020-12-05 05:16

    You get and format like this

    for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
         set dow=%%i
         set month=%%j
         set day=%%k
         set year=%%l
    )
    set datestr=%month%_%day%_%year%
    echo datestr is %datestr%
    
    0 讨论(0)
  • 2020-12-05 05:17

    This will give you DD MM YYYY YY HH Min Sec variables and works on any Windows machine from XP Pro and later.

    @echo off
    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
    set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
    set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
    
    set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
    set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
    echo datestamp: "%datestamp%"
    echo timestamp: "%timestamp%"
    echo fullstamp: "%fullstamp%"
    pause
    
    0 讨论(0)
提交回复
热议问题