computing
  • 1

Dos – How To Convert Month Name As Numbers –

  • 1

Hi,

I want to capture the current date in the format – Month-Day-Year (6-29-11).
Date gives output as 29-Jun-11 and I am modifying as below.

set month=%date:~3,3%
set Day=%date:~0,2%
set year=%date:~7,3%

if %month%==Jan set cmonth=1
if %month%==Feb set cmonth=2
if %month%==Mar set cmonth=3
if %month%==Apr set cmonth=4
if %month%==May set cmonth=5
if %month%==Jun set cmonth=6
if %month%==Jul set cmonth=7
if %month%==Aug set cmonth=8
if %month%==Sep set cmonth=9
if %month%==Oct set cmonth=10
if %month%==Nov set cmonth=11
if %month%==Dev set cmonth=12

Is there any way we can convert the months as numbers ??

Share

1 Answer

  1. Test this… Assumes your current date format is dd/mmm/yy

    @echo off
    cls
    setlocal enabledelayedexpansion
    
    set month=%date:~3,3%
    set Day=%date:~0,2%
    set year=%date:~7,2%
    
    set nbr=0
    for %%1 in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do (
      set /a nbr+=1
      echo %month%|findstr /i /b "%%1" >nul&& goto finis
    )
    
    :finis
    if %nbr% lss 10 set nbr=0%nbr%
    
    echo.&echo.;
    echo The month of %month% is month number %nbr%
    echo.
    echo Current date = %nbr%-%day%-%year% 
    
    

    Please come back & tell us if your problem is resolved.

    • 0