中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-25 23:08
47,812 topics / 349,910 posts / today 0 new / 48,264 members
DOS批处理 & 脚本技术(批处理室) » Batch file for the date N days ago
Printable Version  7,274 / 4
Floor1 namejm Posted 2007-06-07 21:29
荣誉版主 Posts 1,737 Credits 5,226 From 成都
  People often ask how to calculate the date N days ago. Because this involves the issues of big months and small months, common years and leap years, the code becomes relatively complicated and tends to scare people off.

  Although willsort posted code like this two years ago (please refer to: (Ended) Deleting files older than N days from the DOS command line under WINDOWS), and although the idea is extremely simple—convert the date into a relative number, do the subtraction, then convert the relative number back into a date—the numbers 2472632 and 146097 and the like appear too abruptly, making it hard to figure out what's going on. To be honest, limited by my own level, even now I still find it very confusing.

  Now I'm posting three batch files for getting the date N days ago ( the date may include the weekday, but it must be arranged in year-month-day order, and the year must be four digits, otherwise there will be errors). The key parts all have comments, so they're a bit easier to understand than willsort's:

  Code 1:
  Code 2:
  Code 3:

[ Last edited by namejm on 2007-6-8 at 11:15 PM ]
Floor2 vkill Posted 2007-06-07 21:46
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
Using ruby is much simpler. I just want to pursue "using the shortest code, in the fastest time, with the most suitable tool, to solve it"—nothing else intended~

puts Data.today-3

[ Last edited by vkill on 2007-6-7 at 10:28 PM ]
Floor3 HAT Posted 2007-06-07 21:50
版主 Posts 5,017 Credits 9,023
Learning
Posting a batch file that uses VBS to calculate the date
Floor4 wwwyangxm Posted 2008-12-02 09:12
初级用户 Posts 27 Credits 27
I want to ask: in the OP's third method, is set dat=2007-01-02 the initial time? Last night I came up with a method, but I don't know whether it's the same as the OP's. Please clarify. Also, I don't quite understand the OP's code (method three).

Below is my method:

Assume the initial time coordinate is 1900-01-01, and the input time is date1 (any time). This makes it easier to calculate the difference between the two dates (in days), tmp1. Then also input any number of days tmp2 that you want to query before or after date1. If it is N days before date1, then calculate the result as sum=tmp1+tmp2. (tmp2 can be positive or negative: positive for dates after date1, negative for dates before date1.) Then in the end you get a time difference sum measured from the initial time. Based on 1900-01-01 as the start/end time, calculating the date at a difference of sum days should be relatively simple.

This line of thought can solve the date N days before or after any given date. The start/end time can be set according to how large the calculated difference is, so as to reduce the amount of calculation.

Because my knowledge is limited, I can only provide the idea. I hope the experts can give some guidance. I don't know whether my idea is the same as the third method. To be honest, I still haven't completely understood the third method...
Floor5 sea1112 Posted 2008-12-27 11:06
初级用户 Posts 119 Credits 131 From gz
::Determine the day before the current date
::First directly subtract 1 day from the day count
::If the result is day 0, then subtract 1 from the month, and the day count is the last day of that month.
::If the result is month 0, then subtract 1 from the year, and the month count is the last month of that year.
@echo off&setlocal enabledelayedexpansion
set yyyy=%date:~0,4%
set mm=%date:~5,2%
set dd=%date:~8,2%
set /a nd=!dd!-1
::If it's like subtracting one day from March 1 and getting day 0, then...
if !nd!==0 call :dd0
if !mm!==0 call :mm0
set yyyymmdd=!yyyy!-!mm!-!nd!
echo Today is:%date:~0,10%
echo Yesterday was:!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
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023