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 03:17
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 请问如何取下个日期时间 How to get the next date and time View 2,171 Replies 20
Original Poster Posted 2007-04-07 06:25 ·  中国 上海 浦东新区 电信
中级用户
★★
Credits 302
Posts 138
Joined 2007-03-29 02:33
19-year member
UID 83224
Gender Male
Status Offline
Using date to get the time and date. I now want to get the next working day date of the system. For example, today is Friday 20070406, and I want to get next Monday 20070409. If it is Thursday 20070405, I need to get Friday 20070406. I don't know how to implement it. I don't know how to do arithmetic operations on dates, and also don't know how to judge the day of the week.
Floor 2 Posted 2007-04-07 11:12 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
dim Wday,today
today=date
Wday=Weekday(now)
Select Case (Wday)
case 1 today=today+1
case 2 today=today+1
case 3 today=today+1
case 4 today=today+1
case 5 today=today+1
case 6 today=today+3
case 7 today=today+2
end select
WScript.echo "The next working day is: "&CDate(today)

Try this?
Floor 3 Posted 2007-04-07 21:38 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
```batch code
@echo off & setlocal enabledelayedexpansion
set condate=20070407
set today=%date:~0,4%%date:~5,2%%date:~8,2%
set /a num=%today%-%condate%
set /a num=!num!%%7
if %num%==0 set /a num=!today!+2&goto today
if %num%==1 set /a num=!today!+1&goto today
if %num%==2 set /a num=!today!+1&goto today
if %num%==3 set /a num=!today!+1&goto today
if %num%==4 set /a num=!today!+1&goto today
if %num%==5 set /a num=!today!+1&goto today
if %num%==6 set /a num=!today!+3&goto today

:today
echo The next working day is: !num!
pause
```
Floor 4 Posted 2007-04-08 06:10 ·  中国 广东 广州 荔湾区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
There is no universal code..

The formats in XP and 2000 are different.

As for date calculation, it's an algorithm issue. It has nothing to do with here.

As for the day of the week, %date% already contains it.
知,不觉多。不知,乃求知
Floor 5 Posted 2007-04-08 13:58 ·  中国 上海 徐汇区 联通
中级用户
★★
Credits 302
Posts 138
Joined 2007-03-29 02:33
19-year member
UID 83224
Gender Male
Status Offline
Thanks to friend wudixin96. The VBS script on the second floor seems to be usable. There is an issue with the script on the third floor; it will have problems if there are years in between. I wonder if this problem can be solved? Or how I can get the variables of the VBS script in the bat script, is it possible??

[ Last edited by lililulula on 2007-4-8 at 04:27 PM ]
Floor 6 Posted 2007-04-09 05:29 ·  中国 上海 徐汇区 联通
中级用户
★★
Credits 302
Posts 138
Joined 2007-03-29 02:33
19-year member
UID 83224
Gender Male
Status Offline
Bump for myself first, and by the way, digest the script on the 3rd floor~~~
Floor 7 Posted 2007-04-09 07:52 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
It's too cumbersome to write like this..

You can use the system's own functions for analysis...

Assume the operating system is Windows XP..

Then the following script can be used. (Here, error judgment is not carried out, and time judgment is not done on the spot. When using 23:59:59 seconds, it will cause the date to return an error.)

@echo off
set OldDate=%date%
:reInput
echo Please enter the date you want to query. The format is: 2007-04-05.
set /p input=>
echo %input% | date >nul
if not errorlevel 1 (
echo The next working day is: %date:~11,3%
) else (
goto :reInput
)
echo %OldDate% | date >nul
pause
知,不觉多。不知,乃求知
Floor 8 Posted 2007-04-09 07:55 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Oh.. This script can query dates that are any date supported by the XP system. If not used at a specific time point, this script is basically error-free.
知,不觉多。不知,乃求知
Floor 9 Posted 2007-04-09 08:05 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
I haven't looked closely at the title of the original poster. Actually, it's also very simple.

I once posted about arbitrary date calculations, which can query any year, month, and day (similar to an almanac).
But everyone didn't seem to like it, so I deleted the post.
Date calculation needs to define time.
知,不觉多。不知,乃求知
Floor 10 Posted 2007-04-10 00:01 ·  中国 上海 浦东新区 电信
中级用户
★★
Credits 302
Posts 138
Joined 2007-03-29 02:33
19-year member
UID 83224
Gender Male
Status Offline
Friend flyinspace, your script on floor 7 only checks what day of the week the input date is. But I still have questions about the two lines echo %input% | date >nul and echo %OldDate% | date >nul. I don't quite understand. The | date>nul after that is supposed to empty the obtained date? But when I simply test set /p input=:> echo %input% | date>nul, the input date will be modified to the current system date. However, in the script, after adding echo %OldDate% | date >nul, the system date won't be modified. I don't really understand the specific meaning. I hope friend flyinspace can tell me.

And my requirement is to get the system date and then judge the next working day, and get the date. The script on floor 3 will have errors during the year-end, I don't know what methods there are to solve it.
Floor 11 Posted 2007-04-10 01:23 ·  中国 上海 浦东新区 电信
中级用户
★★
Credits 302
Posts 138
Joined 2007-03-29 02:33
19-year member
UID 83224
Gender Male
Status Offline
The problem has been solved. The 2nd floor told me how to do date arithmetic operations. I got inspiration from it. Because the thinking method of the 2nd floor would be wrong when crossing years during script running, so I directly judge the week character, which avoids this problem. The script is as follows (under 2000):
@echo off
set day=%date:~-10,4%%date:~-5,2%%date:~-2,2%
set date=%date:~-14,3%
if %date%==Monday set /a nextworkday=%day%+1&goto workday
if %date%==Tuesday set /a nextworkday=%day%+1&goto workday
if %date%==Wednesday set /a nextworkday=%day%+1&goto workday
if %date%==Thursday set /a nextworkday=%day%+1&goto workday
if %date%==Friday set /a nextworkday=%day%+3&goto workday
if %date%==Saturday set /a nextworkday=%day%+2&goto workday
if %date%==Sunday set /a nextworkday=%day%+1&goto workday
:workday
echo The next working day is: %nextworkday%
pause

There is another problem: why does the variable name have to be date to be effective when setting date multiple times? For example
set date=%date:~0,10%
set date=%date:-=%
The date variable is today's date 20070409
But if it is like this, it is invalid
set day=%date:~0,10%
set day=%date:-=%
The date is 2007-04-09, and the second set is invalid
Enabling delayed variables doesn't help

[ Last edited by lililulula on 2007-4-9 at 12:36 PM ]
Floor 12 Posted 2007-04-10 02:06 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Your system is Windows 2000.

When using my script, there should be an error prompt: no operand.

set date=%date:~-14,3%
Also, it is best not to define existing environment variables.

The error in the next year is because there is no judgment on the year. (There are differences between leap years and common years) And there will be deviations in the first week of each year...

Also, no matter how I look at it, this script should have problems.

Even within 2007, there will definitely be some days with errors.

The time judgment is not strict enough.
知,不觉多。不知,乃求知
Floor 13 Posted 2007-04-10 02:26 ·  中国 上海 浦东新区 电信
中级用户
★★
Credits 302
Posts 138
Joined 2007-03-29 02:33
19-year member
UID 83224
Gender Male
Status Offline
Friend flyinspace, the script you mentioned for the second floor does have issues. If I judge by the day of the week, taking the current day of the week and comparing strings, it should be fine because I'm just taking the system's subsequent day of the week, and there are only 7 possible cases when judging~~~~ Also, I don't quite understand what you mean by not defining existing Huangjing variables?

[ Last edited by lililulula on 2007-4-9 at 01:28 PM ]
Floor 14 Posted 2007-04-10 03:08 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
It is suggested that you read the batch processing tutorial first.

I will point out your problem.

Assume it is April 30, 2007.
Then how to judge?
知,不觉多。不知,乃求知
Floor 15 Posted 2007-04-10 03:59 ·  中国 上海 浦东新区 电信
中级用户
★★
Credits 302
Posts 138
Joined 2007-03-29 02:33
19-year member
UID 83224
Gender Male
Status Offline
You're right when you say that. Later I also saw that the script on floor 2 has this problem, and my script also has this problem. However, the VBS script on floor 3 is okay, as it has the built-in parameter wekekday~~~~ It seems I didn't consider the problem comprehensively. Thanks for the reminder from the upstairs. I want to know if there's a way to solve this problem? Or how to get the value of the variable in the bat script from the final &CDate(today) in VBS???
Forum Jump: