ITPub博客

首页 > 数字化转型 > ERP > 批处理获得系统当前时间之前n天的时间

批处理获得系统当前时间之前n天的时间

原创 ERP 作者:realji 时间:2009-05-28 05:48:50 0 删除 编辑

@echo off
::获得前一天的系统时间
::时间处理部分来自互联网。
SetLocal EnableDelayedExpansion
::if "%1"=="" echo 示例: %0 20080102 5 && pause>nul && exit /B
set dd=%DATE:~8,2%
set mm=%DATE:~5,2%
set yyyy=%DATE:~0,4%
set temp1=%yyyy%%mm%%dd%
::set temp1=20090527
set temp2=1
set YE=%temp1:~0,4%
set MO=%temp1:~4,2%
set DA=%temp1:~6,2%
set DG=%temp2%
for %%i in (YE MO DA DG) do if "!%%i!"=="" echo Caution,Error Format & pause>nul & exit/b
for %%j in (YE MO DA DG) do if !%%j! LEQ 0 echo Caution,Error Format & pause>nul & exit/b
set/a vY1=%YE% %% 400
set/a vY2=%YE% %% 4
set/a vY3=%YE% %% 100
if %vY1%==0 (set var=true) else (
if %vY2%==0 (
if %vY3%==0 (set var=false) else (set var=true)
) else (set var=false )
)
if %DG% GEQ 28 echo Error,Not Support Large Days! & pause>nul & exit /B
set LY=%YE%
set LM=%MO%
if %var%==false (
if %MO%==01 (if %DA% LEQ %DG% (set/a LY=%YE%-1) & (set/a LM=12+%MO%-1) & (set/a LD=31+%DA%-%DG%) else (set/a LD=%DA%-%DG%))
if %MO%==03 (if %DA% LEQ %DG% (set/a LM=%MO%-1) & (set/a LD=28+%DA%-%DG%) else (set/a LD=%DA%-%DG%))
for %%a in (02 04 06 11) do (if "%MO%"=="%%a" (if %DA% LEQ %DG% (set/a LM=%MO%-1) & (set/a LD=31+%DA%-%DG%) else (set/a LD=%DA%-%DG%)))
if %MO%==09 (if %DA% LEQ %DG% (set/a LM=8) & (set/a LD=31+%DA%-%DG%) else (set/a LD=%DA%-%DG%))
for %%b in (05 07 10 12) do (if "%MO%"=="%%b" (if %DA% LEQ %DG% (set/a LM=%MO%-1) & (set/a LD=30+%DA%-%DG%) else (set/a LD=%DA%-%DG%)))
if %MO%==08 (if %DA% LEQ %DG% (set/a LM=7) & (set/a LD=30+%DA%-%DG%) else (set/a LD=%DA%-%DG%))
) else (
if %MO%==01 (if %DA% LEQ %DG% (set/a LY=%YE%-1) & (set/a LM=12+%MO%-1) & (set/a LD=31+%DA%-%DG%) else (set/a LD=%DA%-%DG%))
if %MO%==03 (if %DA% LEQ %DG% (set/a LM=%MO%-1) & (set/a LD=29+%DA%-%DG%) else (set/a LD=%DA%-%DG%))
for %%a in (02 04 06 11) do (if "%MO%"=="%%a" (if %DA% LEQ %DG% (set/a LM=%MO%-1) & (set/a LD=31+%DA%-%DG%) else (set/a LD=%DA%-%DG%)))
if %MO%==09 (if %DA% LEQ %DG% (set/a LM=8) & (set/a LD=31+%DA%-%DG%) else (set/a LD=%DA%-%DG%))
for %%b in (05 07 10 12) do (if "%MO%"=="%%b" (if %DA% LEQ %DG% (set/a LM=%MO%-1) & (set/a LD=30+%DA%-%DG%) else (set/a LD=%DA%-%DG%)))
if %MO%==08 (if %DA% LEQ %DG% (set/a LM=7) & (set/a LD=30+%DA%-%DG%) else (set/a LD=%DA%-%DG%))
)
if %LM% LSS 10 set LM=0%LM:~-1%
if %LD% LSS 10 set LD=0%LD:~-1%
echo %LY%%LM%%LD%
for %%a in (01 02 03 04 05 06 07 08 09) do (
if "%LM%"=="%%a" (
set m= %LM:~1,1%)
)
for %%b in (10 11 12) do (
if "%LM%"=="%%b" (
set m= %LM%)
)
set LM_NEW=%m:~1,2%
echo %LY%%LM_NEW%%LD%

用于日志处理。

对原有的代码进行修改,主要是月份为8月和9月时,计算上一个月时使用set /a=09-1时,会报错:

“无效数字。数字常数只能是十进制(17),十六位进制(0x11)或
八进制(021)。”

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7425608/viewspace-1022546/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2008-01-09

  • 博文量
    55
  • 访问量
    151693