Get Date dd/mon/yyyy

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Get Date dd/mon/yyyy

#1 Post by zagix » 03 Mar 2014 02:45

Hello,

What is the best way to get the date in dd/month/yyyy (03 Mar 2014) in batch script.

Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Get Date dd/mon/yyyy

#2 Post by foxidrive » 03 Mar 2014 03:11

This is close:

Code: Select all

robocopy |find "Started :"

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Get Date dd/mon/yyyy

#3 Post by dbenham » 03 Mar 2014 07:25

See GetTimestamp.bat for time and date processing

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

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Get Date dd/mon/yyyy

#4 Post by carlos » 03 Mar 2014 16:56

there is alternative:
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


jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Get Date dd/mon/yyyy

#5 Post by jfl » 31 Mar 2014 13:56

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

Post Reply