#navi(機能別) ''前日日付を求める等、日時に関する計算'' #contents //////////////////////////////////////////////////////////////////////////// *概要 [#ua5a2903] set /a による数値演算が出来るので、一応、どのような日時計算でも可能である。 ただし、VBSには DateAdd() や DateDiff() といった関数があるので、それらに 頼るのが楽である。~ 関数の仕様については、マイクロソフトサイトの[[オンラインリファレンス:http://www.asia.microsoft.com/japan/msdn/library/default.asp?url=/japan/msdn/library/ja/script56/html/vtorifunctions.asp]]を参照。 //////////////////////////////////////////////////////////////////////////// *前月・翌月の年月を求める [#yc8cac3f] 月の計算であれば閏年の処理が必要無いのでバッチでも簡単に書ける。 set YYYYMM=%DATE:~-10,4%%DATE:~-5,2% set /a LASTMONTH=YYYYMM-1 if %LASTMONTH:~-2%==00 set /a LASTMONTH+=12-100 set /a NEXTMONTH=YYYYMM+1 if %NEXTMONTH:~-2%==13 set /a NEXTMONTH+=100-12 //////////////////////////////////////////////////////////////////////////// *前日・翌日の年月日を求める [#u7cbad32] バッチだけで行うスクリプトを示すが、次項のVBSを使う方法もある。 前日の日付を求める。 set YYYYMMDD=%DATE:~-10,4%%DATE:~-5,2%%DATE:~-2% set/a ANS=YYYYMMDD-1 if not %ANS:~-2%==00 goto ans if %ANS:~4,2%==01 set/a ANS+=-10000+1131&goto ans for %%M in (02 04 06 08 09 11) do if %ANS:~4,2%==%%M set/a ANS+=-100+31&goto ans if not %ANS:~4,2%==03 set/a ANS+=-100+30&goto ans set/a ANS+=-100+28+(!(%ANS:~0,4%%%4)^^!(%ANS:~0,4%%%100)^^!(%ANS:~0,4%%%400)) :ans set /a Y=%DATE:~-10,4%, M=1%DATE:~-5,2%-100, D=1%DATE:~-2%-100-1 if not %D%==0 goto ans if %M%==1 set /a Y-=1, M=12, D=31&goto ans for %%M in (2 4 6 8 9 11) do if %M%==%%M set /a M-=1, D=31&goto ans if not %M%==3 set /a M-=1, D=30&goto ans set /a M-=1, D=28+(!(Y%%4)^^!(Y%%100)^^!(Y%%400)) :ans set /a ANS=Y*10000+M*100+D 翌日の日付を求める。 set YYYYMMDD=%DATE:~-10,4%%DATE:~-5,2%%DATE:~-2% set/a ANS=YYYYMMDD+1 if 1%ANS:~-2% leq 128 goto ans if %ANS:~-2%==32 set/a ANS+=100-31&if not %ANS:~4,2%==12 goto ans if %ANS:~4,2%==13 set/a ANS+=10000-1200&goto ans if %ANS:~4,2%==02 if %ANS:~-2% geq 29 set/a ANS+=2-(!(%ANS:~0,4%%%4)^^!(%ANS:~0,4%%%100)^^!(%ANS:~0,4%%%400))*(31-%ANS:~-2%) for %%M in (02 04 06 09 11) do if %ANS:~-4%==%%M31 set/a ANS+=100-30 :ans set /a Y=%DATE:~-10,4%, M=1%DATE:~-5,2%-100, D=1%DATE:~-2%-100+1 if %D% leq 28 goto ans if %D%==32 set /a M+=1, D=1&if not %M%==12 goto ans if %M%==13 set /a Y+=1, M=1&goto ans if %M%==2 if %D% geq 29 set /a D+=2-(!(Y%%4)^^!(Y%%100)^^!(Y%%400))*(31-D) for %%M in (2 4 6 9 11) do if %M%%D%==%%M31 set /a M+=1, D=1 :ans set /a ANS=Y*10000+M*100+D 閏年判断において、論理NOT演算子(!)とビットXOR演算子(^)を使うことで if文を排除して判断できるようにした。~ なお、遅延展開が有効な時は、! 演算子は ^^! と書く必要がある。 なお、遅延展開が有効な時は、! 演算子は ^^! と書き、 ^ と ! の連続は ^^! でなく^^^^^^! と書く必要がある。 //////////////////////////////////////////////////////////////////////////// *7日前の日付を求める [#n3d7efd4] VBSを使って計算する。 echo WScript.Echo DateAdd("d",-7,Date()) > tmp.vbs for /f "tokens=1-3 delims=/-" %%A in ('cscript //nologo tmp.vbs') do set ANS=%%A%%B%%C del tmp.vbs //////////////////////////////////////////////////////////////////////////// *1時間前の日付時刻を求める [#g8cf03cf] VBSを使って計算する。 echo WScript.Echo DateAdd("h",-1,now()) > tmp.vbs for /f "tokens=1-6 delims=/-: " %%A in ('cscript //nologo tmp.vbs') do set ANS=%%A%%B%%C%%D%%E%%F del tmp.vbs //////////////////////////////////////////////////////////////////////////// #navi(機能別)