1

I have been searching on various sites for converting a date string like Dec17 to December2017, Jul18 to July2018 using Windows batch file. But I am unable to find the exact command matching this requirement.

How to convert short date to long date in a Windows batch file?

Mofi
  • 46,139
  • 17
  • 80
  • 143

2 Answers2

1

Run the batch file below from within a command prompt window without any parameter or with a parameter like Dec17 or Jul18 to see its output on varying short dates.

@echo off
if "%~1" == "" (
    set "ShortDate=Jan18"
) else (
    set "ShortDate=%~1"
)

rem Get abbreviated month by using a string substitution for getting just
rem the first 3 characters from value of environment variable ShortDate.
rem First character has character index 0, 3 is the number of characters.

set "Month=%ShortDate:~0,3%"

rem Get the year by using a string substitution for getting all characters
rem from value of environment variable ShortDate starting from fourth
rem character to end of string value (number of characters not specified).
rem Century 20 is already added here.

set "Year=20%ShortDate:~3%"

if /I "%Month%" == "Jan" set "Month=January"   & goto OutputLongDate
if /I "%Month%" == "Feb" set "Month=February"  & goto OutputLongDate
if /I "%Month%" == "Mar" set "Month=March"     & goto OutputLongDate
if /I "%Month%" == "Apr" set "Month=April"     & goto OutputLongDate
if /I "%Month%" == "May" set "Month=May"       & goto OutputLongDate
if /I "%Month%" == "Jun" set "Month=June"      & goto OutputLongDate
if /I "%Month%" == "Jul" set "Month=July"      & goto OutputLongDate
if /I "%Month%" == "Aug" set "Month=August"    & goto OutputLongDate
if /I "%Month%" == "Sep" set "Month=September" & goto OutputLongDate
if /I "%Month%" == "Oct" set "Month=October"   & goto OutputLongDate
if /I "%Month%" == "Nov" set "Month=November"  & goto OutputLongDate
if /I "%Month%" == "Dec" set "Month=December"  & goto OutputLongDate

:OutputLongDate
echo %Month%%Year%

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %~1.
  • echo /?
  • goto /?
  • if /?
  • rem /?
  • set /?

See also Single line with multiple commands using Windows batch file for an explanation of operator & used to run two commands – SET and GOTO – unconditionally from one command line.

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

Here are some alternative ideas.

The following example uses a For loop and delayed expansion to parse the months:

@Echo Off
SetLocal EnableDelayedExpansion
For %%A In (Long Short) Do Set "%%ADate="

Set /P "ShortDate=Please enter the date in short date format [MMMyy]: "

For %%A In (January February March April May June July August September October
    November December) Do (Set "LongMonth=%%A"
    If /I "%ShortDate:~,3%"=="!LongMonth:~,3!" (
        Set "LongDate=%%A20%ShortDate:~-2%"))

If Defined LongDate Echo %LongDate%
Pause

…and this uses key pairs with variable expansion and replacement:

@Echo Off
Set "kp=Jan-January;Feb-February;Mar-March;Apr-April;May-May;Jun-June;Jul-July"
Set "kp=%kp%;Aug-August;Sep-September;Oct-October;Nov-November;Dec-December"

Set /P "ShortDate=Please enter the date in short date format [MMMyy]: "

Call Set "LongMonth=%%kp:*%ShortDate:~,3%-=%%"
Set "LongMonth=%LongMonth:;="&:"%"
Set "LongYear=20%ShortDate:~-2%"
Set "LongDate=%LongMonth%%LongYear%"

Echo=%LongDate%
Pause

…and the following example gets help from PowerShell:

@Echo Off
Set "SDF=MMMyy"
Set "LDF=MMMMyyyy"
For %%A In (L S) Do Set "%%AD="

Set /P "SD=Please enter the date in short date format [MMMyy]: "

For /F UseBackQ %%A In (`PowerShell^
 "([datetime]::ParseExact('%SD%','%SDF%', [System.Globalization.CultureInfo]::CurrentCulture)).ToString('%LDF%')"
 `) Do Set "LD=%%A"

If Defined LD Echo %LD%
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39