Package script date to variable

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 trying to get the date in the above variables in a batch script, but currently the date is output as

2011/04/

Any suggestions on how to fix this?

+5
source share
8 answers

You will not get what you expected, because it %DATE%returns the current date using the Windows settings for the “short date format”. This setting is fully (infinitely) customizable.

One user can customize their system to display a short date like Fri040811; while another user (even on the same system) can select 04/08/2011. This is a complete nightmare for the BAT programmer.

WMIC. WMIC - WMI WMI. WMI Windows - http://en.wikipedia.org/wiki/Windows_Management_Instrumentation

WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table

FOR.

 FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
    SET /A TODAY=%%F*10000+%%D*100+%%A
 )
+17

. (XP Pro ):

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 ======================================================================

, .

+5

, ,

@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
+3

, XP pro . XP Home wmic.

:: timestamp YYYYMMDD_HHMMSS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set dt=%dt:~0,8%_%dt:~8,6%
echo %dt%
pause

:: timestamp YYYY-MM-DD_HH-MM-SS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%
echo %dt%
pause
+2

,

:: 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%
+1

1 var ( var)?

set ymd=%date:~6,4%/%date:~0,2%/%date:~3,2%
0
for /f %%a in ('wmic os get localdatetime ^| find "."') do set dts=%%a
set ymd=%dts:~0,8%
set hour=%dts:~8,6%
0

The following script will give local time with time zone information (TZ) in both true ISO8601 and human format (without 'T'). It converts the TZ offset in minutes to the required format HHMM, for example, 2019-01-25T08:26:55.347+1300and 2019-01-25 08:26:55.347+1300for 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 delete one of the echo commands

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

0
source

All Articles