码迷,mamicode.com
首页 > 其他好文 > 详细

【转】BAT取当前日期的前一天

时间:2017-12-21 23:01:27      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:命令   处理   命令行   延迟   str   ext1   批处理文件   就是   token   

============================
的日期       OK
============================
@echo off

::的日期,格式化输出
echo ----代码开始----------

echo Wscript.echo dateadd("d",-1,date)>vbs.vbs
for /f %%a in (‘cscript //nologo vbs.vbs‘) do del vbs.vbs&&set yyyymmdd=%%a
for /f "tokens=1,2,3* delims=// " %%i in (‘echo %yyyymmdd%‘) do set yyyy=%%i&set mm=%%j&set dd=%%k
if   %mm%   LSS   9   set   mm=0%mm%
if   %dd%   LSS   9   set   dd=0%dd%
echo %yyyy%++++%mm%++++%dd%

echo -----代码结束---------

::取的日期
echo Wscript.echo dateadd("d",-1,date)>vbs.vbs
for /f %%a in (‘cscript //nologo vbs.vbs‘) do del vbs.vbs&&echo %%a

::当天日期
echo %date:~0,4%%date:~5,2%%date:~8,2%

::如果日期是 - 分割,如2010-06-01
for /f "tokens=1,2,3* delims=- " %%i in (‘date /t‘) do @echo %%i%%j%%k
::如果日期是 / 分割,如2010/06/01
for /f "tokens=1,2,3* delims=// " %%i in (‘date /t‘) do @echo %%i%%j%%k

::当天日期,格式化输出
for /f "tokens=1,2,3* delims=// " %%i in (‘date /t‘) do set yyyy=%%i&set mm=%%j&set dd=%%k
echo %yyyy%++++%mm%++++%dd%


================================
的日期       OK
================================
 

echo off
echo Wscript.echo dateadd("d",-1,date)>vbs.vbs
set day=
for /f %%a in (‘cscript //nologo vbs.vbs‘) do del vbs.vbs&&set day=%day% %%a 
echo %day%

pause


==================================================================

 

下面的代码有问题:当是月初1日的时候,结果会是0,如当前是20100601,结果成了20100600
@echo off
set Today=%date:~0,4%%date:~5,2%%date:~8,2%
set /a PassDays=%Today%-1
echo %PassDays%
pause

==========================

 

 

 

 


test.bat

BatchFile code
@echo off
rem 计算指定天数之前的日期
set DaysAgo=1
rem 假设系统日期的格式为yyyy-mm-dd
call:DateToDays %date:~0,4% %date:~5,2% %date:~8,2% PassDays
set/a PassDays-=%DaysAgo%
call:DaysToDate %PassDays% DstYear DstMonth DstDayset DstDate=%DstYear%-%DstMonth%-%DstDay%
echo %DaysAgo%的日期是%DstDate%
pause
goto :eof
:DateToDays %yy% %mm% %dd% days
set local ENABLEEXTENSIONS
set yy=%1&set mm=%2&set dd=%3
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%)
else (set yy=19%yy%)
  set/a dd=100%dd%%%100,mm=100%mm%%%100
  set/a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
  set/a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
endlocal&set%4=%j%&goto:EOF:DaysToDate %days% yy mm ddsetlocal ENABLEEXTENSIONSset/a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=aset/a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5set/a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10(if %mm% LSS10set mm=0%mm%)&(if %dd% LSS10set dd=0%dd%)endlocal&set%2=%yy%&set%3=%mm%&set%4=%dd%&
goto:EOF


============================================
下面有多个方法
============================================
求N日期的批处理  
 
  经常有人询问N的日期如何计算,因为牵涉到大月小月、平年闰年的问题,代码将会比较复杂,让人望而却步。

  虽然两年willsort曾经发出过这样的代码(请参考:(已结)如果在WINDOWS下DOS命令行删除N以前的文件),尽管思路极其简单:把日期转化为相对数字,做完减法之后,再把相对数字转化为日期,但是,2472632、146097之类的数字来得太过突兀,让人摸不着头脑,说实话,限于水平,我现在都还看得一头雾水。

  现在发三个求N日期的批处理( 日期可以带星期,但是,必须以年月日的顺序排列,年份必须是四位,否则会出错),在关键部分都有注释,比willsort的容易懂一点:

  代码1
CODE:  [Copy to clipboard]
--------------------------------------------------------------------------------

@echo off
:: 求N的日期
:: 思路:
::   模拟手工运算,用日期数减去天数,差值为负时向高位借1
:: 特点:
::   代码简洁,容易理解,速度快慢取决于指定的天数大小

setlocal enabledelayedexpansion

:Main
cls
:: 日期可以带星期,但是,必须以年月日的顺序排列
:: 年份必须是四位,否则会出错
set day=%date%
set days=0
echo.&echo.
echo       指定的日期是:%day%
echo.
set /p input=      请指定要追溯的天数:

:: 提取日期
for /f "tokens=1-3 delims=-/. " %%i in ("%day%") do (
    set /a sy=%%i, sm=100%%j %% 100, sd=100%%k %% 100
)

set /a sd-=input

if %sd% leq 0 call :count

cls
echo.&echo.
echo       指定的日期是:%day%
echo.
set sm=0%sm%
set sd=0%sd%
echo       %input% 的日期是:%sy%-%sm:~-2%-%sd:~-2%
pause>nul
goto Main

:count
set /a sm-=1
if !sm! equ 0 set /a sm=12, sy-=1
call :days
set /a sd+=days
if %sd% leq 0 goto count
goto :eof

:days
:: 获取指定月份的总天数
set /a leap="^!(sy %% 4) & ^!(^!(sy %% 100)) | ^!(sy %% 400)"
set /a max=28+leap
set num=0
set str=31 %max% 31 30 31 30 31 31 30 31 30 31
for %%i in (%str%) do (
    set /a num+=1
    if %sm% equ !num! set days=%%i&goto :eof
)
goto :eof
  代码2:
CODE:  [Copy to clipboard]
--------------------------------------------------------------------------------

@echo off
:: 求N的日期
:: 思路:
::   把当前日期转换为天数
::   用当前天数减去N
::   把结果再转换为日期
:: 具体操作是:
::   逐年计算天数
:: 特点:
::    代码简洁,容易理解,速度较慢,但是不受指定天数大小的影响

setlocal enabledelayedexpansion

:Main
cls
:: 日期可以带星期,但是,必须以年月日的顺序排列
:: 年份必须是四位,否则会出错
set day=%date%
set days=0
echo.&echo.
echo       指定的日期是:%day%
echo.
set /p input=      请指定要追溯的天数:

rem ========把指定日期转换为天数========
:: 提取日期
for /f "tokens=1-3 delims=-/. " %%i in ("%day%") do (
    set /a sy=%%i, sm=100%%j %% 100, sd=100%%k %% 100
)
:: 把年份转化为天数
for /l %%i in (1,1,%sy%) do (
    set /a leap="^!(%%i %% 4) & ^!(^!(%%i %% 100)) | ^!(%%i %% 400)"
    set /a days=days+365+leap
)
:: 把月份转化为天数
set /a num=0, mday=0, max=28+leap
set str=0 31 %max% 31 30 31 30 31 31 30 31 30
for %%i in (%str%) do (
    set /a num+=1
    if !num! leq !sm! set /a mday+=%%i
)

set /a days=days+mday+sd

set /a days-=input


rem ========把天数转换为日期========
:: 获取年份
for /l %%i in (1,1,%sy%) do (
    set /a leap="^!(%%i %% 4) & ^!(^!(%%i %% 100)) | ^!(%%i %% 400)"
    set /a days_tmp=365+leap
    if !days! gtr !days_tmp! (
        set /a days-=days_tmp
        set y=%%i
    )
)
:: 获取月份及日期
set /a m=1, max=28+%leap%
set str=31 %max% 31 30 31 30 31 31 30 31 30
for %%i in (%str%) do (
    if !days! gtr %%i (
        set /a days-=%%i
        set /a m+=1
    ) else goto next
)
:next
set m=0%m%
set d=0%days%

cls
echo.&echo.
echo       指定的日期是:%day%
echo.
echo       %input% 的日期是:%y%-%m:~-2%-%d:~-2%
pause>nul
goto Main
  代码3:
CODE:  [Copy to clipboard]
--------------------------------------------------------------------------------

@echo off
:: 求N的日期
:: 思路:
::   把当前日期转换为天数
::   用当前天数减去N
::   把结果再转换为日期
:: 具体操作是:
:    先按照每年365计算,然后再计算闰年的个数
::   最后再做转换
:: 特点:
::   代码复杂,不便于理解,但是速度很快
:: 计算上还存在bug,可以用 11111 来和头两个做对比测试,暂时没时间更新

setlocal enabledelayedexpansion

:Main
cls
:: 日期可以带星期,但是,必须以年月日的顺序排列
:: 年份必须是四位,否则会出错
set day=2007-01-02
set /a days=0, sum=0
echo.&echo.
echo       指定的日期是:%day%
echo.
set /p input=      请指定要追溯的天数:

rem =======把日期转换为天数=======
:: 提取日期
for /f "tokens=1-3 delims=/-. " %%i in ("%day%") do (
    set /a sy=%%i, sm=100%%j %% 100, sd=100%%k %% 100, y=%%i
)

:: 把年份抓换为天数
set num=0
if %sy% lss 400 goto next1
call :leap_num
:next1
set /a leap_num=sy/4-num

:: 把月份转换为天数
set /a leap="^!(sy %% 4) & ^!(^!(sy %% 100)) | ^!(sy %% 400)"
set /a max=28+leap
set num=0
set str=31 %max% 31 30 31 30 31 31 30 31 30 31
for %%i in (%str%) do (
    set /a num+=1
    if !num! lss %sm% set /a sum+=%%i
)

set /a days=365*sy+leap_num+sum+sd

set /a days-=input

rem =======把天数转换为日期=======

:: 获取年份
set /a y=days/365
set /a mod1=days %% 365
call :leap_num
set /a mod2=mod1-leap_num
:loop2
if %mod2% lss 0 (
    set /a y-=1
    set /a mod2=365+mod2
) else goto next2
goto loop2

:: 获取月份及日期
:next2
set /a m=1, days=mod2+1, max=28+%leap%
set str=31 %max% 31 30 31 30 31 31 30 31 30
for %%i in (%str%) do (
    if !days! gtr %%i (
        set /a days-=%%i
        set /a m+=1
    ) else goto next3
)
:next3
set m=0%m%
set d=0%days%

cls
echo.&echo.
echo       指定的日期是:%day%
echo.
echo       %input% 的日期是:%y%-%m:~-2%-%d:~-2%
pause>nul
goto Main

:leap_num
:: 计算能被100整除但是不能被400整除的年次
set /a y_tmp=(%y:~0,1%%y%-%y%)/10
set num=0
:loop1
set /a mod=y_tmp %% 400
if %mod% neq 0 set /a num+=1
set /a y_tmp-=100
if %y_tmp% geq 100 goto loop1
set /a leap_num=y/4-num
goto :eof
[ Last edited by namejm on 2007-6-8 at 11:15 PM ]


发一个使用VBS计算日期的批处理
CODE:  [Copy to clipboard]
--------------------------------------------------------------------------------

@echo off
rem 删除三的文件夹
rem 目标目录为C:\test
set Target=C:\test
dir /b %Target%>%temp%\FolderList.txt
echo dt=date()-3>%temp%\OldDate.vbs
echo s=right(year(dt),4) ^& right("0" ^& month(dt),2) ^&  right("0" ^& day(dt),2)>>%temp%\OldDate.vbs
echo wscript.echo s>>%temp%\OldDate.vbs
for /f %%f in (‘cscript /nologo %temp%\OldDate.vbs‘) do @set DelFlag=%%f
for /f %%i in (%temp%\FolderList.txt) do (
  if %%i lss %DelFlag% (
    rd /s /q %Target%\%%i
    )
  )
del %temp%\FolderList.txt
del %temp%\OldDate.vbs

 
 


::判断当前日期的
::首先直接把天数减1
::如果出来的是0就把月减1,天数是当月的最后
::如果出来的是0月就把年减1年,月数是当年的最后一月。
@echo off&setlocal enabledelayedexpansion
set yyyy=%date:~0,4%
set mm=%date:~5,2%
set dd=%date:~8,2%
set /a nd=!dd!-1
::如果象3月1日减是0就...
if !nd!==0 call :dd0
if !mm!==0 call :mm0
set yyyymmdd=!yyyy!-!mm!-!nd!
echo 今天是:%date:~0,10%
echo 昨天是:!yyyymmdd!
pause

:dd0
set /a mm=!mm!-1
for %%a in (1 3 5 7 8 10 12)do set %%add=31
set /a pddd=!yyyy!*10/4
set pd2d=!pddd:~-1,1!
set 2dd=28
if !pd2d!==0 set 2dd=29
for %%b in (4 6 9 11)do set %%bdd=30
set nd=!%mm%dd!
goto :eof
:mm0
set /a yyyy=!yyyy!-1
set mm=12
set nd=31
goto :eof

 
 

=========================================================

 [原创]取的日期的bat脚本

这个是我些日子刚写的bat中的部分, 这是我第次写bat脚本, 那段日子成天泡在联盟中学习bat的知识, 非常感谢联盟的兄弟们!
由于批处理脚本在自动上传文件中使用的非常广泛, 取的日期是完成该任务大多会碰到的问题, 所以把代码贴在这里供像我一样的新手参考.
我的方法有一点取巧之嫌: 如果当天是1号, 那么需要处理月(也许有年), 然后列举一月最后的日, 比如10月就一定是31, 但是2月就要特殊处理, 需要计算是否为闰年, 具体的计算方法可以在我的代码中看到, 这个想必难不倒大家, 最后将日期拼就是的日期啦.
希望大家有更好的方法把代码贴出来分享一下:)
CODE:  [Copy to clipboard]
--------------------------------------------------------------------------------

::--------Generate Last Date----------
echo Generate last datetime...

set TodayYear=%date:~0,4%
set TodayMon=%date:~5,2%
set TodayDay=%date:~8,2%

set LastdayYear=
set LastdayMon=
set LastdayDay=

set LastdayYear=%TodayYear%
set LastdayMon=%TodayMon%
set /A LastdayDay=TodayDay-1

if "%LastdayDay%" == "0" (
        if "!LastdayMon!" == "01" (
                set LastdayMon=12
                set /A LastdayYear-=1
        ) else (
                set /A LastdayMon-=1
                
                if "!LastdayMon!" == "1" set LastdayMon=01
                if "!LastdayMon!" == "2" set LastdayMon=02
                if "!LastdayMon!" == "3" set LastdayMon=03
                if "!LastdayMon!" == "4" set LastdayMon=04
                if "!LastdayMon!" == "5" set LastdayMon=05
                if "!LastdayMon!" == "6" set LastdayMon=06
                if "!LastdayMon!" == "7" set LastdayMon=07
                if "!LastdayMon!" == "8" set LastdayMon=08
                if "!LastdayMon!" == "9" set LastdayMon=09
        )
        
        if "!LastdayMon!" == "01" set LastdayDay=31
        if "!LastdayMon!" == "03" set LastdayDay=31
        if "!LastdayMon!" == "04" set LastdayDay=30
        if "!LastdayMon!" == "05" set LastdayDay=31
        if "!LastdayMon!" == "06" set LastdayDay=30
        if "!LastdayMon!" == "07" set LastdayDay=31
        if "!LastdayMon!" == "08" set LastdayDay=31
        if "!LastdayMon!" == "09" set LastdayDay=30
        if "!LastdayMon!" == "10" set LastdayDay=31
        if "!LastdayMon!" == "11" set LastdayDay=30
        if "!LastdayMon!" == "12" set LastdayDay=31
        
        if "!LastdayMon!" == "02" (
                set IsLeapYear=
                
                set /A IsLeapYear=!LastdayYear!%%400
                if "!IsLeapYear!" == "0" (
                        set LastdayDay=29
                        goto MAKELASTDATE
                )
                
                set /A IsLeapYear=!LastdayYear!%%100
                if "!IsLeapYear!" == "0" (
                        set LastdayDay=28
                        goto MAKELASTDATE
                )
                
                set /A IsLeapYear=!LastdayYear%%4
                if "!IsLeapYear!" == "0" (
                        set LastdayDay=29
                        goto MAKELASTDATE
                ) else (
                        set LastdayDay=28
                        goto MAKELASTDATE
                )
        )
)

:MAKELASTDATE

if "%LastdayDay%" == "1" set LastdayDay=01
if "%LastdayDay%" == "2" set LastdayDay=02
if "%LastdayDay%" == "3" set LastdayDay=03
if "%LastdayDay%" == "4" set LastdayDay=04
if "%LastdayDay%" == "5" set LastdayDay=05
if "%LastdayDay%" == "6" set LastdayDay=06
if "%LastdayDay%" == "7" set LastdayDay=07
if "%LastdayDay%" == "8" set LastdayDay=08
if "%LastdayDay%" == "9" set LastdayDay=09

set LastDate=%LastdayYear%.%LastdayMon%.%LastdayDay%

echo Generate last datetime OK!

 
 


俺用FOR /L给你改了下 权当自己练习~~~~应该简练一些~~


  Quote: 
::--------Generate Last Date----------
echo Generate last datetime...

set TodayYear=%date:~0,4%
set TodayMon=%date:~5,2%
set TodayDay=%date:~8,2%

set LastdayYear=
set LastdayMon=
set LastdayDay=

set LastdayYear=%TodayYear%
set LastdayMon=%TodayMon%
set /A LastdayDay=TodayDay-1


if "%LastdayDay%" == "0" (
        if "!LastdayMon!" == "01" (
                set LastdayMon=12
                set /A LastdayYear-=1
        ) else (
                set /A LastdayMon-=1
                for /l %%a in (1,1,9) do if !LastdayMon! == %%a (set LastdayMon=0%%a)
        )

 

  for /l %%a in (1,2,7) do (
      if !LastdayMon! equ %%a (set set LastdayDay=31) 
      else
      for /l %%a in (8,2,12) do if !LastdayMon! equ %%a (set set LastdayDay=31)
      else
      if "!LastdayMon!" == "02" (
                                 set IsLeapYear=
                
                                  set /A IsLeapYear=!LastdayYear!%%400
                                  if "!IsLeapYear!" == "0" (
                                                            set LastdayDay=29
                                                            goto MAKELASTDATE
                                                           )
                
                                  set /A IsLeapYear=!LastdayYear!%%100
                                  if "!IsLeapYear!" == "0" (
                                                            set LastdayDay=28
                                                            goto MAKELASTDATE
                                                           )
                
                                  set /A IsLeapYear=!LastdayYear%%4
                                  if "!IsLeapYear!" == "0" (
                                                            set LastdayDay=29
                                                            goto MAKELASTDATE
                                                           ) 
                                   else (
                                         set LastdayDay=28
                                         goto MAKELASTDATE
                                        )
                                 )
      else set LastdayDay=30
  )
)

:MAKELASTDATE
for /l %%a in (1,1,9) do if %LastdayDay% == 1 (set LastdayDay=0%%a)

set LastDate=%LastdayYear%.%LastdayMon%.%LastdayDay%

echo Generate last datetime OK! 
 

 

 
 

 


应该在批处理文件的前面加入
setlocal enabledelayedexpansion
表示启用变量延迟,否则这个程序不行正常执行,我已试过。
最后感谢你的分亨,帮我解决了问题。

 
 
 只有启用了变量延迟,才能用!!将变量名扩起来表示对变量值的引用。

 
 
如果有数据库连接的话,可以从数据库里取sysdate-1,然后写到一个文件中,再从文件中将的日期取出即可,,,前提 要能连数据库 嘎嘎

 @echo off
echo Wscript.echo dateadd("d",-1,date)>vbs.vbs
for /f %%a in (‘cscript //nologo vbs.vbs‘) do del vbs.vbs&&echo %%a

【转】BAT取当前日期的前一天

标签:命令   处理   命令行   延迟   str   ext1   批处理文件   就是   token   

原文地址:http://www.cnblogs.com/yachao1120/p/8082525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!