How-to: Get the current Date - GetDate.cmd

Display the date and time independent of OS Locale, Language or the users chosen date format (Control Panel/Regional).

If all you need is the current date and time in local format, you can just use the built in variables %date% and %time% the more difficult problem is making this work for any locale so that it can be used across international networks.

Method 1 - Calling a VBScript getdate.vbs

@Echo off
For /f %%G in ('cscript /nologo getdate.vbs') do set _dtm=%%G
Set _yyyy=%_dtm:~0,4%
Set _mm=%_dtm:~4,2%
Set _dd=%_dtm:~6,2%
Set _hh=%_dtm:~8,2%
Set _nn=%_dtm:~10,2%
Echo %_yyyy%-%_mm%-%_dd%T%_hh%:%_nn%

Method 2 - Calling WMIC

@Echo off
:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error

:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
   IF "%%~L"=="" goto s_done
      Set _yyyy=%%L
      Set _mm=00%%J
      Set _dd=00%%G
      Set _hour=00%%H
      SET _minute=00%%I
)
:s_done

:: Pad digits with leading zeros
      Set _mm=%_mm:~-2%
      Set _dd=%_dd:~-2%
      Set _hour=%_hour:~-2%
      Set _minute=%_minute:~-2%

:: Display the date/time in ISO 8601 format:
Set _isodate=%_yyyy%-%_mm%-%_dd% %_hour%:%_minute%
Echo %_isodate%

GOTO:EOF

:s_error
Echo GetDate.cmd
Echo Displays date and time independent of OS Locale, Language or date format.
Echo:
Echo Returns 6 environment variables containing isodate,Year,Month,Day,hour and minute.
Based on the sorted date code by Rob van der Woude.

This works under Windows 7/Windows 10 independent of the short date format / Operating System Language/ Locale.

"I've been on the calendar, but never on time" ~ Marilyn Monroe

Related

datetime.vbs - Get Date, Time and daylight savings (VB Script)
DOFF.exe - Print a formatted date and time, with an optional date offset, e.g. -1 prints yesterday, +1 prints tomorrow. (source + BSD licence).
Get UNIX format date - Dave Benham / StackOverflow (WMIC)
Standard date and time notation - ( YYYY-MM-DD )
Date/Time scripts - Rob van der Woude.
Date/Time scripts - Ritchie Lawrence.


 
Copyright © 1999-2020 SS64.com
Some rights reserved