Hello,
What is the best way to get the date in dd/month/yyyy (03 Mar 2014) in batch script.
Thanks in advance.
Get Date dd/mon/yyyy
Moderator: DosItHelp
Re: Get Date dd/mon/yyyy
See GetTimestamp.bat for time and date processing
The following simply prints the value to the screen
This version saves the result in a dt variable
Use of the -mth option allows definition of month abbreviations for other languages. Here is a Norwegian example
Dave Benham
The following simply prints the value to the screen
Code: Select all
call getTimestamp -f "{dd} {mth} {yyyy}"
This version saves the result in a dt variable
Code: Select all
call getTimestamp -f "{dd} {mth} {yyyy}" -r dt
Use of the -mth option allows definition of month abbreviations for other languages. Here is a Norwegian example
Code: Select all
call getTimestamp -f "{dd} {mth} {yyyy}" -mth "jan febr mars april mai juni juli aug sept okt nov des"
Dave Benham
Re: Get Date dd/mon/yyyy
there is alternative:
I adapted the code from here: http://www.dostips.com/forum/viewtopic.php?p=29758#p29758
I adapted the code from here: http://www.dostips.com/forum/viewtopic.php?p=29758#p29758
Code: Select all
@echo off
call :getdate.init
call :getdate
if %day% leq 9 set "day=0%day%"
echo %day% %monthstr% %year%
pause
goto :eof
:getdate.init
set /a "jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12"
set /a "mon=1,tue=2,wed=3,thu=4,fri=5,sat=6,sun=7"
(echo .set infheader=""
echo .set infsectionorder=""
echo .set inffooter="%%2"
for /l %%# in (1,1,4) do echo .set inffooter%%#=""
echo .set cabinet="off"
echo .set compress="off"
echo .set donotcopyfiles="on"
echo .set rptfilename="nul"
) >"%temp%\~foo.ddf"
goto :eof
:getdate
set "tf=%temp%\~%random%"
makecab /d inffilename="%tf%" /f "%temp%\~foo.ddf" >nul
for /f "usebackq tokens=1-7 delims=: " %%a in ("%tf%") do (
set "monthstr=%%b"
set /a "year=%%g,month=%%b,day=1%%c-100,weekday=%%a"
set /a "hour=1%%d-100,minute=1%%e-100,second=1%%f-100")
del "%tf%" >nul 2>&1
goto :eof
Re: Get Date dd/mon/yyyy
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:
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
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