问题
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
回答1:
Use
"tokens=2-4delims=. "
Note the SPACE before the closing "
回答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:

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 namedDayOfWeek
%date%
produces display of date on your system, likepet 28.06.2013
:~,3
takes a substring, starting at the first (index 0) of 3 characters (Fri)- SET Day= creates the
Day
environmental variableDay
:~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%
回答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%
回答4:
For a reliable date stamp this uses WMIC in XP Pro and above.
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
pause
来源:https://stackoverflow.com/questions/17369004/separate-day-month-and-year-from-a-date-batch-file