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 Settings).

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
:: GetDate1.cmd
:: Display date and time independent of OS Locale, Language or date format.
Setlocal
For /f %%A in ('cscript /nologo getdate.vbs') do set _dtm=%%A 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 The current date/time is: %_yyyy%-%_mm%-%_dd%@%_hh%:%_nn% :: Save the date/time in ISO 8601 format: Endlocal&Set _dtm=%_YYYY%-%_mm%-%_dd%@%_hh%-%_nn%

Method 2 - Calling Robocopy

@Echo off
:: GetDate2.cmd
:: Display date and time independent of OS Locale, Language or date format.
Setlocal
For /f "tokens=1-6 delims=/: " %%A in ('robocopy "|" . /njh ^| find ":"') do  (
    set "_YYYY=%%A" & set "_month=%%B" & set "_day=%%C" 
    set "_hour=%%D" & set "_min=%%E"   & set "_sec=%%F"
)
Echo The current date/time is: %_YYYY%-%_month%-%_day%@%_hour%:%_min%:%_sec%

:: Save the date/time in ISO 8601 format:
Endlocal&Set _dtm=%_YYYY%-%_month%-%_day%@%_hour%-%_min%

:: Based on the lengthy forum threads here and here.

Extending this to rename a file is as simple adding the variable where needed - see StampMe.cmd:
REN "somefile.txt" "somefile%_dtm%.txt
"

Method 3 - Calling date /t, this provides the date only.

@Echo off
:: GetDate3.cmd
:: Display date independent of OS Locale, Language or date format.
Setlocal
Set t=2&if "%date%z" LSS "A" set t=1
For /f "skip=1 tokens=2-4 delims=(-)" %%A in ('echo/^|date') do (
  for /f "tokens=%t%-4 delims=.-/ " %%J in ('date/t') do (
    set %%A=%%J&set %%B=%%K&set %%C=%%L)
)
Echo The current date is year: %yy% month:%mm% day: %dd%
Endlocal&set _yyyy=%yy%&set _mm=%mm%&set _dd=%dd%

:: Adapted from Ritchie Lawrence GetDate

Method 4 - Calling DOFF.exe

@Echo off
:: GetDate4.cmd
:: Display date and time independent of OS Locale, Language or date format.
For /f %%A in ('DOFF.exe yyyy-mm-dd@hh-min') do set _dtm=%%A
Echo The current date/time is: %_dtm%

Calling a 3rd party executable even a freeware one, has the disadvantage that you need to install it and make sure it's available, but this does have the advantage that it won’t unpredictably change format with a Windows upgrade. See below for links to DOFF.exe.

Method 5 - Calling PowerShell.exe, this can be slow to launch as PowerShell requires .Net to be loaded.

@Echo off
:: GetDate5.cmd
:: Display date and time independent of OS Locale, Language or date format.
For /f "delims=" %%A in ('powershell get-date -format "{yyyy-MMM-dd@HH:mm}"') do @set _dtm=%%A
Echo The current date/time is: %_dtm%

:: Adapted from npocmaka's answer on Stack overflow.

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

Related commands

datetime.vbs - Get Date, Time and daylight savings (VB Script).
DOFF.exe - Print a formatted date and time by John Fitzgibbon, optional date offset e.g. -1 prints yesterday. (source + BSD licence).
How-to: Standard date and time notation - ( YYYY-MM-DD )
PowerShell equivalent: get-date -format yyyy-MM-ddTHH-mm-ss-ff
Date/Time scripts - Rob van der Woude.
Date/Time scripts - Ritchie Lawrence.


 
Copyright © 1999-2024 SS64.com
Some rights reserved