separate day month and year from a date batch file

10,907

Solution 1

Use

"tokens=2-4delims=. "

Note the SPACE before the closing "

Solution 2

This will do it based on the local date on your computer. I'm using offsets based on my local computer date, which is Fri 06/28/2013 - you can adjust for yours as shown below):

Mine (in dateparse.bat):

@ECHO OFF
@ECHO.
@ECHO Date is %Date%
SET DayOfWeek=%Date:~0,3%
SET Day=%Date:~7,2%
SET Month=%Date:~4,2%
SET Year=%Date:~10,4%
SET Today=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%
@ECHO Year is %Year%, Month is %Month%, Day is %Day%, DayOfWeek is %DayOfWeek% 
@ECHO Today is %Today%
@ECHO.

Output:

Output of dateparse.bat

Explanation (first two assignments, with the rest left to you) - note that the offset of the output is zero based, so the first character is index 0, the second is index 1, and so forth:

  • SET DayOfWeek= creates an environmental variable named DayOfWeek
  • %date% produces display of date on your system, like pet 28.06.2013
  • :~,3 takes a substring, starting at the first (index 0) of 3 characters (Fri)
  • SET Day= creates the Day environmental variable Day
  • :~7,2 takes a substring, starting at position 8 (index 7), of 2 characters (28)

Yours (untested - you may need to adjust):

SET DayOfWeek=%Date:%~0,3%
SET Day=%Date:~4,2%
SET Month=%Date:~7,2%
SET Year=%Date:~10,4%
ECHO %Year% %Month% %Day% %DayOfWeek% %Today%

Solution 3

Add a space as a delimiter.

FOR /F "tokens=1-4 delims=. " %%A IN ("%sample%") DO (
    SET day=%%B
    SET month=%%C
    SET year=%%D
)
echo %day%
echo %month%
echo %year%
Share:
10,907
Adrianna Mayo
Author by

Adrianna Mayo

Updated on July 30, 2022

Comments

  • Adrianna Mayo
    Adrianna Mayo almost 2 years

    when i do

    echo %date%
    

    i get output as

    pet 28.06.2013
    

    other dates

    pon -> monday
    tor -> tuesday 
    sre -> wedneday 
    cet -> thursday 
    pet -> friday 
    sob -> saturday 
    ned -> sunday
    

    I wrote following batch file to get the day month and time. but its not working. please help/

    FOR /F "tokens=1-3 delims=." %%A IN ("%sample%") DO (
        SET day=%%A
        SET month=%%B
        SET year=%%C
    )
    echo %day%
    echo %month%
    echo %year%
    

    output

    pet 28
    06
    2013
    

    I want the output as

    28
    06
    2013