How can I get the date in a locale independent format in a batch file?

Solution 1:

It sounds like a bad idea to be parsing a date that could be in various formats which format you don't know. Better to get the date in a specific format, even if that means not pure batch but invoking powershell.

C:\Users\tod>for /f %a in ('C:\Windows\System32\WindowsPowershell\v1.0\powershell -Command "Get-Date -format yyyy_MM_dd__HH_mm_ss"') do set datetime=%a

C:\Users\tod>set datetime=2018_01_22__09_15_33

or without time.

C:\Users\tod>for /f %a in ('C:\Windows\System32\WindowsPowershell\v1.0\powershell -Command "Get-Date -format yyyy_MM_dd"') do set datetime=%a

C:\Users\tod>set datetime=2018_01_22

C:\Users\tod>

And by the way that Year/Month/Day i.e. yyyy/mm/dd format is very nice as it is easily human-readable in both the UK and USA. No worries about if it's dd/mm/yy like uk. Or mm/dd/yy. You see e.g. 2018 first so you know it's year first and then you know that after the year is the month and then the day.

Once you have that date in the format you kmow, and I suggest yyyy/mm/dd format, you can do

C:\Users\tod>echo %datetime:~0,4%
2018

C:\Users\tod>

And so on.. So you can set whatever variable to equal the year, month, date in month.

Added

If you wanted to not use powershell and you don't mind that this will only work for US formatted dates.

C:\Users\tod>echo %date%
01/22/2018

C:\Users\tod>echo %date:~0,2%
01

C:\Users\tod>echo %date:~3,2%
22

C:\Users\tod>echo %date:~6,4%
2018

C:\Users\tod>

So you really should be able to see how to do it

set first=%date:~0,2%

set second=%date:~3,2%

set third=%date:~6,4%


C:\Users\tod>set first=%date:~0,2%

C:\Users\tod>set second=%date:~3,2%

C:\Users\tod>set third=%date:~6,4%

C:\Users\tod>echo %first%/%second%/%third%
01/22/2018

C:\Users\tod>echo %second%/%first%/%third%
22/01/2018

C:\Users\tod>

So you can say set ukdate=%second%/%first%/%third% and you can say echo %ukdate%

And that non-powershell method only works if you know the format of the date on the system, and know that it's US. I would generally recommend against batch, because for example what if a new revision of the OS comes along and the output of a command is a bit different and your code presumes a particular format in its parsing. And while in this case batch is in one sense, neat, sometimes it can be like banging sticks together. It's a good idea to know batch and to use it a bit but to install some other scripting language like Ruby or Python or Golang or NodeJS. But if you want to use solely batch for this, then there it is.

Solution 2:

How can I get the date in a batch file in a locale independent format?

I need to be displayed with this format dd/mm/yyyy

First, what not to do:

  • Using %date% to provide a solution is, as used in the other answer, dependent on the OS Locale, Regional, and Language settings and may provide wrong or missing information.

  • For example, the short date format on my PC does not output the weekday at all.

The right way to solve the problem:

  • Using wmic, on the other hand, works independently of OS Locale, Language or the user's chosen date format (Control Panel/Regional).

The following batch file uses wmic to retrieve the date and (local) time, so doesn't suffer the disadvantage of a solution using %date%.

getdate.cmd:

@echo off
setlocal
rem get the date
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1-3" %%g in (`wmic Path Win32_LocalTime Get Day^,Month^,Year ^| findstr /r /v "^$"`) do (
  set _day=00%%g
  set _month=00%%h
  set _year=%%i
  )
rem pad day and month with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
rem output format required is DD/MM/YYYY
echo %_day%/%_month%/%_year%
endlocal

Example output:

> getdate
22/01/2018

Modify your batch file as appropriate to use the above code, for example:

@echo off
setlocal
rem get the date
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1-3" %%g in (`wmic Path Win32_LocalTime Get Day^,Month^,Year ^| findstr /r /v "^$"`) do (
  set _day=00%%g
  set _month=00%%h
  set _year=%%i
  )
rem pad day and month with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
rem output format required is DD/MM/YYYY
set source=%path%.csv
echo %source%
set dest=%path%_%_day%/%_month%/%_year%.csv
echo %dest%
endlocal

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • for /f - Loop command against the results of another command.
  • getdate - Display the date and time independent of OS Locale, Language or the users chosen date format (Control Panel/Regional).
  • variables - Extract part of a variable (substring).
  • wmic - Windows Management Instrumentation Command.