中国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-04 12:49
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » How can I get the previous day's date in a batch file under 98 or 2000?
Printable Version  1,340 / 7
Floor1 xolbm Posted 2005-12-18 21:55
新手上路 Posts 2 Credits 8
Under 98 or 2000, for example if the current system date is 20051218, then how can I get the previous day's date 20051217 in a batch file?
Floor2 无奈何 Posted 2005-12-19 21:51
荣誉版主 Posts 356 Credits 1,338
This problem of yours is super complicated. You need to consider whether the current date is the 1st; if it is, then you also have to consider how many days were in the previous month, and whether it's a leap year or a common year. Here's an example for getting the date under 2000, try improving it yourself!



  1. ::Get the date
  2. rem Please improve it yourself
  3. @echo off
  4. for /f "tokens=1,2,3 delims=-/. " %%a in ('date /t') do (
  5. set A=%%a
  6. set B=%%b
  7. set C=%%c
  8. )
  9. rem set /a C-=1
  10. echo %A%%B%%C%
-=Code coloring BY:无奈何=-
Floor3 tigerpower Posted 2005-12-19 22:38
中级用户 Posts 99 Credits 377
Use GNU date:

.\date -d "-1 day" "+%Y%m%d"

Attachments
date.zip (18.99 KiB)
Floor4 willsort Posted 2005-12-19 22:53
元老会员 Posts 1,512 Credits 4,432
Re xolbm:

This question has different answers on different platforms.

Under 2000's cmd, you can convert the date into a relative value from some earlier date, then do arithmetic on that value, and then convert it back into the new date.

Under 98's command, you can use a marker method: each day when the program runs, mark today's date, and take yesterday's date from the previous mark. But this algorithm requires the program to be run at least once every day.

A more general method is to use a third-party command-line program, such as date, but then it loses the characteristics of a batch script program.

Since this topic has been discussed many times before, the code and programs for the above solutions can all be found in older posts, such as the link below, so please make good use of the search function:

(Solved) How to delete files older than N days from the DOS command line under WINDOWS
http://www.cn-dos.net/forum/viewthread.php?tid=16676
Floor5 无奈何 Posted 2005-12-19 23:00
荣誉版主 Posts 356 Credits 1,338
Originally posted by tigerpower at 2005-12-19 22:38:
Use GNU date:

.\date -d "-1 day" "+%Y%m%d"

Haha, using a different tool makes it much simpler. Those little tools under UNIX really are powerful.
Floor6 xolbm Posted 2005-12-20 19:31
新手上路 Posts 2 Credits 8
Thank you, teachers, for your guidance! It looks like using a batch program to add or subtract system time is quite difficult for a newbie like me!
Thanks again, teachers, for your guidance!!
Floor7 LiveOnLove Posted 2005-12-20 19:51
中级用户 Posts 161 Credits 493 From ZHCN
Yeah. The batch commands above already made me dizzy. Better to just write a small program directly, much more convenient. HOHO
Floor8 wynpu Posted 2007-04-30 06:17
新手上路 Posts 3 Credits 6
Great, I'll try it first
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023