Batch script date into variable

后端 未结 8 1432
终归单人心
终归单人心 2020-12-01 22:34
for /F \"tokens=1-4 delims=/ \" %%i in (\'date /t\') do (
set Day=%%k
set Month=%%j
set Year=%%l
set DATE=%%k/%%j/%%l)

I am try to get the date int

相关标签:
8条回答
  • 2020-12-01 23:06

    The following script will give local time with timezone (TZ) information, in both true ISO8601 and human format (no 'T'). It converts the TZ offset in minutes into the HHMM format needed e.g. 2019-01-25T08:26:55.347+1300 and 2019-01-25 08:26:55.347+1300 for NZ with DST.

    @echo off
    for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
    set ccyy_mm_dd=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
    set hh_mm_ss=%ldt:~8,2%:%ldt:~10,2%:%ldt:~12,2%
    set _fff=%ldt:~14,4%
    set tzsign=%ldt:~21,1%
    set tzmins=%ldt:~22%
    set /a tzHH=(%tzmins%/60)
    set /a tzMM=(%tzmins%-(%tzHH%*60))
    set /a tzHH=100 + %tzHH%
    set tzHH=%tzHH:~1,2%
    set /a tzMM=100 + %tzMM%
    set tzMM=%tzMM:~1,2%
    set ldt=%ccyy_mm_dd% %hh_mm_ss%%_fff%%tzsign%%tzHH%%tzMM%
    set ldt8601=%ccyy_mm_dd%T%hh_mm_ss%%_fff%%tzsign%%tzHH%%tzMM%
    echo %ldt%
    echo %ldt8601%
    

    You probably want to remove one of the echo commands

    EDIT for those wanted a colon in the TZ, change %tzHH%%tzMM% to %tzHH%:%tzMM%

    0 讨论(0)
  • 2020-12-01 23:07

    I think this is what you want:

    @echo off
    :MENU
    CLS
    for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set xsukax=%%a
    echo Year=%xsukax:~0,4%
    echo Month=%xsukax:~4,2%
    echo Day=%xsukax:~6,2%
    echo hour=%xsukax:~8,2%
    echo Minutes=%xsukax:~10,2%
    echo seconds=%xsukax:~12,2%
    
    pause
    goto MENU
    
    0 讨论(0)
  • 2020-12-01 23:08
    for /f %%a in ('wmic os get localdatetime ^| find "."') do set dts=%%a
    set ymd=%dts:~0,8%
    set hour=%dts:~8,6%
    
    0 讨论(0)
  • 2020-12-01 23:09

    Couldn't you simply use the following 1 line to create your var (using any var name)?

    set ymd=%date:~6,4%/%date:~0,2%/%date:~3,2%
    
    0 讨论(0)
  • 2020-12-01 23:11

    I have derived the shortest from the already given solutions. This works on every system (XP Pro and up):

    REM ===================================================================
    REM CREATE UNIQUE DATETIME STRING IN FORMAT YYYYMMDD-HHMMSS
    REM ======================================================================
    FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
    SET DateTime=%DTS:~0,8%-%DTS:~8,6%
    REM ======================================================================
    

    Of course you can play with the resulting string format.

    0 讨论(0)
  • 2020-12-01 23:20

    Feel free to use this any way you want

    :: Date in year, day, month format
    
    FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO (
        FOR /f "tokens=2 delims= " %%A IN ('date /t') DO (
            SET v_first=%%G
            SET v_second=%%H
            SET v_third=%%I
            SET v_all=%%A
            )
        )
    
    SET %v_first%=%v_all:~0,2%
    SET %v_second%=%v_all:~3,2%
    SET %v_third%=%v_all:~6,4%
    SET DATE2= %MM%_%DD%_%YY%
    ECHO. The date is: %DATE2%
    
    0 讨论(0)
提交回复
热议问题