有什么简单的方法可以使用批处理脚本CDM获得2天前的日期? [英] Is there any easy way to get 2 days ago date using batch scripting CDM?

查看:133
本文介绍了有什么简单的方法可以使用批处理脚本CDM获得2天前的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改代码,以便使用批处理文件获取两天前的日期.在此示例中,我可以获取今天的日期

I am trying to change the code in order to get two days ago date using the batch file. In this example, I can take today date

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set LogDateTime=%datetime:~0,8%_%datetime:~8,6%

但是,我不知道如何修改代码以获取两天前的数据.我不是在寻找PowerShell,也没有其他解决方案.我们只需要使用批处理即可.

However, I don't know how to modify the code to get two days ago data. I am not looking for PowerShell, and not any other solution. We have to use only the batch.

推荐答案

您需要将日期转换为儒略日期才能在批处理文件中进行日期数学计算.这是两个功能.一个获取当前日期,然后一个进行该日期数学运算.

You need to convert the date into a Julian Date to do date math in batch files. Here are two function. One to get the current date and then one to do that date math.

@echo off
setlocal

REM Get the current date
REM Will return variables YY, YYYY, MM, DD, HH, Min and Sec
Call :GetDateTime

REM Add or Subtract from the current date
REM Must use + or - symbol
REM Revised date will be assigned to RetVar
Call :AddSubDate %YYYY% %MM% %DD% -2 past

echo Past     : %past%
pause
GOTO :EOF

:AddSubDate Year Month Day <+/-Days> RetVar
setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2% & set DD=%DD:~-2%
set ret=%YYYY: =%%MM: =%%DD: =%
endlocal & set %~5=%ret%
exit /b

:GetDateTime Year Month Day Hour Minute Second
@echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
   set "YY=%YY%" 
   set "YYYY=%YYYY%" 
   set "MM=%MM%" 
   set "DD=%DD%"
   set "HH=%HH%" 
   set "Min=%Min%"
   set "Sec=%Sec%"
)
exit /b

这篇关于有什么简单的方法可以使用批处理脚本CDM获得2天前的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆