2016-03-22
@echo off REM 取得日期 FOR /F "tokens=1-4 delims=/ " %%a IN ("%date%") DO ( rem SET _MyDate=%%a%%b%%c%%d SET MyDate=%%a-%%b-%%c ) FOR /F "tokens=1-4 delims=: " %%a IN ("%time%") DO ( SET MyTime=%%a-%%b-%%c SET Hour=%%a SET Minute=%%b SET Second=%%c SET Second=%Time:~6,2% ) echo Hour=%Hour% echo Minute=%Minute% echo Second=%Second% rem 測試環境 : Windows 10 rem 小時不會自動補0,只好另外處理,但分會自動補0 IF %Hour:~0,1% == 0 ( SET Hour=%Hour% echo "1" ) ELSE ( SET Hour=0%Hour% echo "2" ) echo Hour=%Hour% SET MyDateTime=%MyDate%-%MyTime% SET MyDateTime2=%MyDate%-%Hour%-%Minute%-%Second% REM 顯示去掉分隔符號後的結果 echo %MyDate% echo %MyTime% echo %MyDateTime% echo %MyDateTime2% rem ************************************************************** echo ************************************************************** REM 取得今天的年、月、日 (自動補零) SET TodayYear=%date:~0,4% SET TodayMonthP0=%date:~5,2% SET TodayDayP0=%date:~8,2% REM 取得今天的年、月、日 (純數字) rem 修正 Batch 遇到 08, 09 會視為八進位的問題 IF %TodayMonthP0:~0,1% == 0 ( SET /A TodayMonth=%TodayMonthP0:~1,1%+0 ) ELSE ( SET /A TodayMonth=TodayMonthP0+0 ) IF %TodayMonthP0:~0,1% == 0 ( SET /A TodayDay=%TodayDayP0:~1,1%+0 ) ELSE ( SET /A TodayDay=TodayDayP0+0 ) echo 日期 %TodayYear%/%TodayMonth%/%TodayDay% echo 日期 %TodayYear%/%TodayMonthP0%/%TodayDayP0% REM 取得時、分、秒 、豪秒 (自動補零) SET Hour=%Time:~0,2% echo Hour=%Hour% SET Minute=%Time:~3,2% echo Minute=%Minute% SET Second=%Time:~6,2% echo Second=%Second% SET /A Millisecond=%Time:~9,2%*10 echo Millisecond=%Millisecond% echo 時間 %Hour%:%Minute%:%Second%.%Millisecond% IF %HourP0:~0,1% == 0 ( SET /A Hour=%HourP0:~1,1%+0 ) ELSE ( SET /A Hour=HourP0+0 ) echo 時間 %Hour%:%Minute%:%Second%.%Millisecond% |
參考
如何在 Batch 檔取得系統的日期、時間欄位 (第三版)
http://blog.miniasp.com/post/2009/11/03/How-to-get-system-date-time-in-batch-file-part-III.aspx
如何利用批次檔取得今日的日期或時間,甚至是部分的日期或時間
http://alexchuo.blogspot.tw/2006/07/blog-post.html
沒有留言:
張貼留言