

Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre".NET." NOT for ASP.NET 1.0, 1.1, or 2.0 
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .





February 18th, 2005, 02:52 PM

Friend of Wrox


Join Date: Jun 2003
Location: , , USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts


DateFunctions
How do I get the difference in years, months and days from two dates? Example:
Date1 = 12/31/2004
Date2 = 11/30/2002
The result should give me the difference between this two dates.
I can get use the DateDiff function to get how many years or months or days but the problem is I need the result in years, months and days. I am sorry if I am getting you confused.
the result should be:
dtDiff = 2years/1month/1day
Thanks,
Judy

February 18th, 2005, 04:02 PM

Friend of Wrox


Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts


Use DateDiff to find the difference in years, months and days and combine your results. something like this
Diff_years = DateDiff("yyyy",Date_1,Date_2)
Diff_Months = DateDiff("m",Date_1,Date_2)
Diff_Days = DateDiff("d",Date_1,Date_2)
dtDiff = Diff_years & " years/" & Diff_Months & " months/" Diff_Days & " days"

February 19th, 2005, 11:10 PM

Friend of Wrox


Join Date: Jun 2003
Location: , , USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts


shahchi1,
If I use the datediff function I will have:
dtDiff = 2/years/25 months/731 days
This is why I was saying that the DateDiff function might not work because if I do as you suggested,
Diff_years = 2
Diff_months = 25
diff_days = 731
Datediff will give you only what you are asking for specifically whether it is for years OR months OR days not the difference by Years AND Months AND Days.
I was hoping I can find out how to go about this instead of going the long way.
Thanks,
Judy

February 21st, 2005, 01:28 AM

Friend of Wrox


Join Date: Oct 2003
Location: Cochin, Kerala, India.
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts


Here is a relatively simple algorithm for doing this.
fromdate should be before todate. If else indicate this and exit.
Seperateout the date, month and year parts of both dates (fromdate and todate)into 6 variables say date1, month1, year1, date2, month2 and year2.
yd=year2year1
md=month2month1
dd=date2date1
You can notice the following observations.
1) yd will never be less than 0.
2) If yd=0, then month difference will not be less than zero.
3) if yd=0 and md=0, then dd is greater than zero.
if(yd>0) then
if(md>0) then
if(dd<0) then
md=md1
dd=getlastdateofmonth(month21, year2)date1+date2
end if
elseif(md=0) then
if(dd<0) then
yd=yd1
md=md+11
dd=getlastdateofmonth(month21, year2)date1+date2
end if
else
yd=yd1
md=md+12
if(dd<0) then
md=md1
dd=getlastdateofmonth(month21, year2)date1+date2
end if
end if
else ' yd=0
if(dd<0) then
md=md1
dd=getlastdateofmonth(month21, year2)date1+date2
end if
end if
Where the function getlastdateofmonth gives the last date of given month. You can very easily write such a function. Please take into consideration whether year2 is leap year.
The output is
yd YEARS , md MONTHS and dd DAYS
For e.g. fromdate=141103 and todate=240905 gives
1 year, 10 months and 10 days
fromdate=240903 and todate=141103 gives
0 Yeras, 1 month and 21 days

February 21st, 2005, 06:41 PM

Friend of Wrox


Join Date: Jun 2003
Location: , , USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts


Thanks for your help. I will follow this logic.

Thread Tools 

Display Modes 
Linear Mode

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off




