Batch Get DateTime year, month, day, hour, min, sec (12-feb-2021)


In a batch file I needed to extract a year, month, day, hour, min and sec from the date command. So I used the following, which essentially parses the Date command to extract its sub strings into a variable DateTime.

Use the WMIC function in new Windows to get the date, this will work independent on of the region setting on the windows server.

Create a BAT file to use this commands:
________________________________
@echo off

REM DATETIME
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set DateTime=%%a
set year=%DateTime:~0,4%
set month=%DateTime:~4,2%
set day=%DateTime:~6,2%
set hour=%DateTime:~8,2%
set min=%DateTime:~10,2%
set sec=%DateTime:~12,2%

set complete_datetime=%year%-%month%-%day% %hour%.%min%.%sec%
echo %complete_datetime%
________________________________

Output: 2021-02-12 07.39.19


Bekijk meer nieuws >>