Regional date format tool (DOS English)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 91
Joined: 02 Oct 2012 17:21

Regional date format tool (DOS English)

#1 Post by carlsomo » 18 Aug 2013 10:50

This batch only script can be useful to obtain the regional date format as well as assign date variables.
I have only tested it on Win7 64 bit
I'm sure it would be easy to modify for other languages:

Code: Select all

@echo off&goto :start
::                                  ::
::——————|   RegDateFmt.bat   |——————::
::                                  ::
::——————¯¯¯¯¯¯ by  carl ¯¯¯¯¯¯——————::

:RegDateFmt Format Separator yy mm dd day mon
color 1e
echo(%~nx0 ^<Format^> [Separator] [yy] [mm] [dd] [day] [mon]
echo(Assigns 'Format' variable in the current regional date setting:
echo(        ie. 'Day mm-dd-yyyy', 'dd-Mon-yy' or 'mm/dd/yyyy'
echo(Assigns 'Format_dte' to the regional date display ie. 'Sun 08/18/2013'
echo(Assigns 'Separator' variable as '/' or '-' or '.' or ','
echo(Assigns yy=year mm=month dd=day, if passed as 3rd - 5th arguments
echo(Assigns Day of week if included in current format with 6th arg
echo(Assigns 3 letter month if included in current format with 7th arg
echo(Returns numeric date: yyyymmdd in errorlevel
echo('Format_dte' can be used to check if corresponds to current date, ie:
echo(        If "%%date%%" neq "%%Format_dte%%" call %~nx0 Format
echo(Assign all potential date variables:
echo(  %~nx0 format sep yy mm dd day mon
echo(  echo mm=%%mm%% dd=%%dd%% yy=%%yy%% mdy=%%mm%%/%%dd%%/%%yy%% ymd=%%yy%%%%mm%%%%dd%%
echo(Get a date stamp for a file name:
echo(  %~nx0 format
echo(  set/a ymd=%%errorlevel%%
echo(%cmdcmdline%|find /i "/c " >nul
if %errorlevel% equ 0 (
  <nul set/p="Press any key to close this window..."
exit/b 1

if "%~1"=="" goto :RegDateFmt
if "%~1"=="/?" goto :RegDateFmt
SetLocal EnableDelayedExpansion
set "Format="
for /f "tokens=2 delims=:" %%A in ('date^<nul') do if not defined Format set Format=%%A
set "Format=%Format: =%"
for %%A in (/ - . ,) do (
  echo %Format%|find "%%A">nul&if !errorlevel! equ 0 set "Separator=%%A"
if /i "%Format:~0,1%" gtr "9" (
  set "Day=Day "&set "dy=%Format:~0,3% "
) else set "Day="&set "dy="
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('date^<nul') do (
  set "Format=%Day%%%a%Separator%%%b%Separator%%%c"
  for /f "tokens=1-3 delims=.-/ " %%d in ("%date:* =%") do (
    set %%a=%%d&set %%b=%%e&set "%%c=%%f"
    echo !yy:~3!|find "ECHO">nul
    if /i !errorlevel! neq 0 (
      set "Format=!Format:yy=yyyy!"
    ) else set "yyyy=20!yy!"
    if /i "!mm:~0,1!" gtr "9" (
      set "Format=!Format:mm=Mon!"
      set "Mon=!mm!"
      call :month_convert mm
      if /i "!errorlevel!" neq "0" exit/b 1
    ) else set "Mon="
if /i 1%mm% lss 20 set "mm=0%mm%
if /i 1%dd% lss 20 set "dd=0%dd%
set "Format_dte=%Format:Day =!dy!%"
set "Format_dte=%Format_dte:mm=!mm!%"
set "Format_dte=%Format_dte:Mon=!Mon!%"
set "Format_dte=%Format_dte:yyyy=!yy!%"
set "Format_dte=%Format_dte:yy=!yy!%"
set "Format_dte=%Format_dte:dd=!dd!%"
  set "%~1=%Format%"
  set "%~1_dte=%Format_dte%"
  if not "%~2"=="" set "%~2=%Separator%"
  if not "%~3"=="" set "%~3=%yy%"
  if not "%~4"=="" set "%~4=%mm%"
  if not "%~5"=="" set "%~5=%dd%"
  if not "%~6"=="" if not "%day%"=="" set "%~6=%dy:~0,3%"
  if not "%~7"=="" if not "%Mon%"=="" set "%~7=%Mon%"
)&exit/b %yyyy%%mm%%dd%

set "months=Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
set "month=!%~1!"
set/a mnum=0
for %%m in (%months%) do set/a mnum+=1&if /i %month% equ %%m call set "%~1=!mnum!"&exit /b 0
exit/b 1

Post Reply