Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 18th, 2005, 03:52 PM
Friend of Wrox
Points: 1,035, Level: 12
Points: 1,035, Level: 12 Points: 1,035, Level: 12 Points: 1,035, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default 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

Reply With Quote
  #2 (permalink)  
Old February 18th, 2005, 05:02 PM
Friend of Wrox
 
Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"
Reply With Quote
  #3 (permalink)  
Old February 20th, 2005, 12:10 AM
Friend of Wrox
Points: 1,035, Level: 12
Points: 1,035, Level: 12 Points: 1,035, Level: 12 Points: 1,035, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default

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
Reply With Quote
  #4 (permalink)  
Old February 21st, 2005, 02:28 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Cochin, Kerala, India.
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

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=year2-year1
md=month2-month1
dd=date2-date1

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=md-1
            dd=getlastdateofmonth(month2-1, year2)-date1+date2
        end if
    elseif(md=0) then
        if(dd<0) then
            yd=yd-1
            md=md+11
            dd=getlastdateofmonth(month2-1, year2)-date1+date2
        end if
    else
        yd=yd-1
        md=md+12
        if(dd<0) then
            md=md-1
            dd=getlastdateofmonth(month2-1, year2)-date1+date2
        end if
    end if
else ' yd=0
    if(dd<0) then
        md=md-1
        dd=getlastdateofmonth(month2-1, 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=14-11-03 and todate=24-09-05 gives

1 year, 10 months and 10 days

fromdate=24-09-03 and todate=14-11-03 gives

0 Yeras, 1 month and 21 days
Reply With Quote
  #5 (permalink)  
Old February 21st, 2005, 07:41 PM
Friend of Wrox
Points: 1,035, Level: 12
Points: 1,035, Level: 12 Points: 1,035, Level: 12 Points: 1,035, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default

Thanks for your help. I will follow this logic.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off



All times are GMT -4. The time now is 07:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.