I wrote the following routine a couple of years ago.
It's easy to use its output to generate the format you wish, or any other variation.
In your case, use:
Code: Select all
call :Now
echo %DAY%/%MONTH%/%YEAR%
The "Now" routine is a pure-batch attempt at parsing the date and time in a way compatible with any language and locale.
It forces the output variables widths to fixed widths, suitable for use in ISO 8601 date/time format strings.
Note that it would have been much easier to cheat and do all this by invoking a PowerShell command!
The major difficulty is that the cmd.exe date and time are localized, and the year/month/day order and separator vary a lot between countries and languages.
Workaround: Use the short date format from the registry as a template to analyse the date and time strings.
Tested in English, French, German, Spanish, Simplified Chinese, Japanese.
In case of problem, set DEBUG=1 to enable debug output.
Jean-François
Code: Select all
:#----------------------------------------------------------------------------#
:# #
:# Function Now #
:# #
:# Description Locale-independant routine to parse the current date/time #
:# #
:# Returns Environment variables YEAR MONTH DAY HOUR MINUTE SECOND MS#
:# #
:# Notes This routine is a pure-batch attempt at parsing the date #
:# and time in a way compatible with any language and locale.#
:# Forces the output variables widths to fixed widths, #
:# suitable for use in ISO 8601 date/time format strings. #
:# Note that it would have been much easier to cheat and #
:# do all this by invoking a PowerShell command! #
:# #
:# The major difficulty is that the cmd.exe date and time #
:# are localized, and the year/month/day order and separator #
:# vary a lot between countries and languages. #
:# Workaround: Use the short date format from the registry #
:# as a template to analyse the date and time strings. #
:# Tested in English, French, German, Spanish, Simplified #
:# Chinese, Japanese. #
:# #
:# Uses %TIME% and not "TIME /T" because %TIME% gives more: #
:# %TIME% returns [H]H:MM:SS.hh #
:# "TIME /T" returns MM:SS only. #
:# #
:# Set DEBUG=1 before calling this routine, to display #
:# the values of intermediate results. #
:# #
:# History #
:# 2012-02-14 JFL Created this routine. #
:# #
:#----------------------------------------------------------------------------#
:now
setlocal enableextensions enabledelayedexpansion
:# First get the short date format from the Control Panel data in the registry
for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate 2^>NUL ^| findstr "REG_SZ"') do set "SDFTOKS=%%a"
if .%DEBUG%.==.1. echo set "SDFTOKS=!SDFTOKS!"
:# Now simplify this (ex: "yyyy/MM/dd") to a "YEAR MONTH DAY" format
for %%a in ("yyyy=y" "yy=y" "y=YEAR" "MMM=M" "MM=M" "M=MONTH" "dd=d" "d=DAY" "/=-" ".=-" "-= ") do set "SDFTOKS=!SDFTOKS:%%~a!"
if .%DEBUG%.==.1. echo set "SDFTOKS=!SDFTOKS!"
:# From the actual order, generate the token parsing instructions
for /f "tokens=1,2,3" %%t in ("!SDFTOKS!") do set "SDFTOKS=set %%t=%%a&set %%u=%%b&set %%v=%%c"
if .%DEBUG%.==.1. echo set "SDFTOKS=!SDFTOKS!"
:# Then get the current date and time. (Try minimizing the risk that they get off by 1 day around midnight!)
set "D=%DATE%" & set "T=%TIME%"
if .%DEBUG%.==.1. echo set "D=%D%" & echo set "T=%T%"
:# Remove the day-of-week that appears in some languages (US English, Chinese...)
for /f %%d in ('for %%a in ^(%D%^) do @^(echo %%a ^| findstr /r [0-9]^)') do set "D=%%d"
if .%DEBUG%.==.1. echo set "D=%D%"
:# Extract the year/month/day components, using the token indexes set in %SDFTOKS%
for /f "tokens=1,2,3 delims=/-." %%a in ("%D%") do (%SDFTOKS%)
:# Make sure the century is specified, and the month and day have 2 digits.
set "YEAR=20!YEAR!" & set "YEAR=!YEAR:~-4!"
set "MONTH=0!MONTH!" & set "MONTH=!MONTH:~-2!"
set "DAY=0!DAY!" & set "DAY=!DAY:~-2!"
:# Remove the leading space that appears for time in some cases. (Spanish...)
set "T=%T: =%"
:# Split seconds and milliseconds
for /f "tokens=1,2 delims=,." %%a in ("%T%") do (set "T=%%a" & set "MS=%%b")
if .%DEBUG%.==.1. echo set "T=%T%" & echo set "MS=%MS%"
:# Split hours, minutes and seconds. Make sure they all have 2 digits.
for /f "tokens=1,2,3 delims=:" %%a in ("%T%") do (
set "HOUR=0%%a" & set "HOUR=!HOUR:~-2!"
set "MINUTE=0%%b" & set "MINUTE=!MINUTE:~-2!"
set "SECOND=0%%c" & set "SECOND=!SECOND:~-2!"
set "MS=!MS!000" & set "MS=!MS:~0,3!"
)
if .%DEBUG%.==.1. echo set "YEAR=%YEAR%" ^& set "MONTH=%MONTH%" ^& set "DAY=%DAY%" ^& set "HOUR=%HOUR%" ^& set "MINUTE=%MINUTE%" ^& set "SECOND=%SECOND%" ^& set "MS=%MS%"
endlocal & set "YEAR=%YEAR%" & set "MONTH=%MONTH%" & set "DAY=%DAY%" & set "HOUR=%HOUR%" & set "MINUTE=%MINUTE%" & set "SECOND=%SECOND%" & set "MS=%MS%" & goto :eof