Reputation: 10081
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 into the above variables in a batch script, but currently the date comes out as
2011/04/
Any suggestions on how to fix this?
Upvotes: 6
Views: 42487
Reputation: 989
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
Upvotes: 3
Reputation: 1221
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%
Upvotes: 0
Reputation: 399
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%
Upvotes: 1
Reputation: 28
for /f %%a in ('wmic os get localdatetime ^| find "."') do set dts=%%a
set ymd=%dts:~0,8%
set hour=%dts:~8,6%
Upvotes: 0
Reputation: 671
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.
Upvotes: 8
Reputation: 41257
This is what I'd use in an XP pro machine and higher. XP Home does not have 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
and another
:: 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
Upvotes: 3
Reputation:
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%
Upvotes: 1
Reputation: 29359
You don't get what you expected because %DATE%
returns the current date using the windows settings for the "short date format". This setting is fully (endlessly) customizable.
One user may configure its system to show the short date as Fri040811; while another user (even in the same system) may choose 08/04/2011. It's a complete nightmare for a BAT programmer.
One possible solution is to use WMIC, instead. WMIC is the WMI command line interface to WMI. WMI Windows Management Instrumentation is the http://en.wikipedia.org/wiki/Windows_Management_Instrumentation
WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table
returns the date in a convenient way to directly parse it with a FOR
.
Completing the parse and putting the pieces together
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
)
Upvotes: 18