China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-04 11:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How can I get the previous day's date in a batch file under 98 or 2000? View 1,339 Replies 7
Original Poster Posted 2005-12-18 21:55 ·  中国 四川 乐山 电信
新手上路
Credits 8
Posts 2
Joined 2005-12-17 21:20
20-year member
UID 47351
Status Offline
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?
Floor 2 Posted 2005-12-19 21:51 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
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:无奈何=-
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 3 Posted 2005-12-19 22:38 ·  中国 上海 闵行区 电信
中级用户
★★
大师兄
Credits 377
Posts 99
Joined 2005-08-26 07:37
20-year member
UID 41945
Status Offline
Use GNU date:

.\date -d "-1 day" "+%Y%m%d"
Attachments
date.zip (18.99 KiB, Credits to download 1 pts, Downloads: 69)
Floor 4 Posted 2005-12-19 22:53 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2005-12-19 23:00 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
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.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 6 Posted 2005-12-20 19:31 ·  中国 四川 乐山 电信
新手上路
Credits 8
Posts 2
Joined 2005-12-17 21:20
20-year member
UID 47351
Status Offline
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!!
Floor 7 Posted 2005-12-20 19:51 ·  中国 广东 珠海 电信
中级用户
★★
Credits 493
Posts 161
Joined 2002-10-29 00:00
23-year member
UID 94
Gender Male
From ZHCN
Status Offline
Yeah. The batch commands above already made me dizzy. Better to just write a small program directly, much more convenient. HOHO
Floor 8 Posted 2007-04-30 06:17 ·  中国 广东 珠海 电信
新手上路
Credits 6
Posts 3
Joined 2007-04-30 05:55
19-year member
UID 87002
Gender Male
Status Offline
Great, I'll try it first
Forum Jump: