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-07-02 15:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Challenge 5]: Calculation of Time Before and After a Date [Difficulty: ☆☆] View 4,769 Replies 27
Floor 16 Posted 2007-05-02 17:52 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
I have an idea, which is to take the remainder... divide by 365, the remainder Y1, then first take the remainder of Y1 divided by 4 (for leap years) to see how many leap years there are, and subtract several 1s from Y1 to get Y2. Then convert Y2 into months and days... (Haha, still execute is quite lazy)
Floor 17 Posted 2007-05-02 18:10 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Oh. Looked carefully. Indeed considered leap years. .

Principle research in progress?
知,不觉多。不知,乃求知
Floor 18 Posted 2007-05-02 18:24 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by slore at 2007-5-2 04:52 AM:
I have an idea, which is to use the remainder... divide by 365, the remainder Y1, then first take the remainder of Y1 divided by 4 (for leap years) to see how many leap years there are, and then subtract that number of 1s from Y1 to get Y2.

Then convert Y2 into months and days...
(Haha, still execute is quite lazy)


Now regardless of whether you are lazy or not...

Please come up with the code that everyone needs.

In this regard, please don't be lazy no matter what.
知,不觉多。不知,乃求知
Floor 19 Posted 2007-05-02 18:28 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Oh, it seems like it's called New Script File.vbs. It seems to have been deleted. Then let's rewrite it, okay?
Floor 20 Posted 2007-05-02 18:36 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by slore at 2007-5-2 05:28 AM:
Oh my god, it seems to be called New Script File.vbs

It seems to be deleted.. Then let's rewrite it = =


Hehe, thanks for your hard work:)

Looking forward to it.
知,不觉多。不知,乃求知
Floor 21 Posted 2007-05-02 20:41 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline

Dim ADYear
Dim ADMonth
Dim ADDay
Dim ADDDay
Dim RnMod

Dim Mon(12)

Dim strDate, strADDay
strDate = "2007-3-4"
strADDay = 800

ADYear = Year(strDate)
RnMod = ADYear Mod 4
If RnMod = 0 Then tempRNYear = 1
ADMonth = Month(strDate)
ADDay = Day(strDate)

strADDay = strADDay + Date2Day(ADYear, ADMonth, ADDay)
tempYear = strADDay \ 365
tempRNday = strADDay Mod 365
If tempYear <> 0 Then
tempRNYear = tempRNYear + (RnMod + tempYear) \ 4
tempRNday = tempRNday - tempRNYear
End If

ADYear = ADYear + tempYear
MsgBox Day2Date(ADYear, tempRNday)


Function Date2Day(ByVal y, ByVal m, ByVal d)
Mon(1) = 31: Mon(2) = 28: Mon(3) = 31: Mon(4) = 30: Mon(5) = 31: Mon(6) = 30
Mon(7) = 31: Mon(8) = 31: Mon(9) = 30: Mon(10) = 31: Mon(11) = 30: Mon(12) = 31
Date2Day = 0

If IsDate(y & "/2/29") Then Mon(2) = 29
Dim i
For i = 1 To m - 1
Date2Day = Date2Day + Mon(i)
Next
Date2Day = Date2Day + d
End Function

Function Day2Date(ByVal y, ByVal Days)
Mon(2) = 28
If IsDate(y & "/2/29") Then Mon(2) = 29
Dim i

If Days > 31 Then
For i = 1 To 11
If Days < Mon(i) Then Exit For
Days = Days - Mon(i)
Next
ADMonth = i

Else
ADMonth = 1
End If
Day2Date = y & "-" & ADMonth & "-" & Days
End Function


Because there are 97 leap years every 400 years, there is a situation where there is a leap year every 8 years. So if 2000 is the initial year, don't calculate the days after 146000, because it will be off by 1 or 2 days, because I calculate based on a leap year every 4 years.

Here I only wrote the part of adding days... You can modify the part of subtracting days by yourself... I wrote this just to present my idea with code...
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flyinspace +3 2007-05-02 21:26
Floor 22 Posted 2007-05-02 20:45 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
8000 days is also very fast... Hehe

Add below strADDay=800:
execute("msgbox #" & strDate & "#+" & strADDay) to verify...

10000 is still correct.. 2000000 is a mess... I won't change it... As long as the idea is clear, it's OK
Floor 23 Posted 2007-05-02 21:27 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Just tested it.. still relatively standard...

What I wrote is not enough.. I'll replace it with a new one later...
知,不觉多。不知,乃求知
Floor 24 Posted 2007-06-08 08:56 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
When organizing the index post, I found this post. Coincidentally, I posted a thread asking for the date N days ago yesterday. Please refer to here: Batch script to find the date N days ago
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 25 Posted 2007-06-08 10:44 ·  IANA 局域网IP(Private-Use)
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Originally posted by flyinspace at 2007-5-2 05:34 PM:

The algorithm is very simple.

Also smarter than mine... But because it didn't consider the existence of leap years.

There will be problems when calculating longer periods of days...

Adding a leap year judgment will be much better.


Didn't look closely at willsort's code;
But caught this line;

dd+y*365+y/4-y/100+y/400

This line shows that leap years are considered;
Isn't this usage a very classic algorithm?
Floor 26 Posted 2007-06-12 23:55 ·  中国 广东 深圳 龙岗区 电信
新手上路
Credits 15
Posts 7
Joined 2006-06-23 13:36
20-year member
UID 57456
Status Offline
A lot of code is not understandable.... Urgently studying...
Floor 27 Posted 2008-04-17 17:19 ·  中国 浙江 杭州 电信
新手上路
Credits 2
Posts 1
Joined 2008-04-17 16:52
18-year member
UID 116164
Gender Male
Status Offline
Good
Floor 28 Posted 2009-11-28 13:55 ·  中国 北京 联通
新手上路
Credits 4
Posts 4
Joined 2009-11-24 00:25
16-year member
UID 155228
Gender Male
Status Offline
Thanks in advance
Forum Jump: