Last active
August 29, 2015 14:02
-
-
Save aetos382/538556e22d590eee92c1 to your computer and use it in GitHub Desktop.
バッチファイルで指定日の前日を求める関数っぽいもの
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo on | |
set today=%date% | |
rem コメントアウトを外して、好きな日付に設定してテストしてね | |
rem set today=2000/03/01 | |
rem / 区切りで分割する | |
for /f "tokens=1,2,3 delims=/" %%i in ("%today%") do ( | |
set year=%%i | |
set month=%%j | |
set day=%%k | |
) | |
rem 日が 1 より大きければ、日を 1 減らして終了 | |
if %day% gtr 1 ( | |
set /a day -= 1 | |
goto :RESULT | |
) | |
rem 月が 1 より大きければ、月を 1 減らして末日をセットして終了 | |
if %month% gtr 1 ( | |
set /a month -= 1 | |
call :GET_LAST | |
goto :RESULT | |
) | |
rem 年を 1 減らして末日を求める | |
set /a year -= 1 | |
set month=12 | |
call :GET_LAST | |
:RESULT | |
rem 月と日が 1 桁ならば前に 0 を補う | |
if "%month:~1,1%" == "" set month=0%month% | |
if "%day:~1,1%" == "" set day=0%day% | |
rem 結果表示 | |
echo YESTERDAY = %year%/%month%/%day% | |
exit /b | |
:GET_LAST | |
rem 末日を求める関数 | |
for /f "tokens=%month% delims=," %%i in ("31,28,31,30,31,30,31,31,30,31,30,31") do set day=%%i | |
rem 2 月でなければ閏年の計算はしない | |
if %month% NEQ 2 GOTO :GET_LAST_EXIT | |
set /a remainder1=%year% %% 4 | |
set /a remainder2=%year% %% 100 | |
set /a remainder3=%year% %% 400 | |
rem 4 で割り切れない年は閏年ではない | |
if %remainder1% NEQ 0 GOTO :GET_LAST_EXIT | |
rem 100 で割り切れる年は、400 で割り切れないならば閏年ではない | |
if %remainder2% EQU 0 if %remainder3% NEQ 0 GOTO :GET_LAST_EXIT | |
rem 日付に 1 を足す | |
set /a day += 1 | |
:GET_LAST_EXIT | |
exit /b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment