Solved

DateDiff Fucntion

Posted on 2004-10-16
12
623 Views
Last Modified: 2012-06-27
Hi,

The following code retrive number of days passed between two date.

dateDiff("d",rst("date1"),rst("date2"))

How can I retrieve; suppose the days passed between two date retrive 400 days then I want to display like as following format:

1 year 1 month 5 days

Could anyone help me to do it?

ayha1999
0
Comment
Question by:ayha1999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 12328749
I'd probably do something like this:

If datediff("y",rst("date1"), rst("date2")) > 0 Then
   If datediff("y",rst("date1"), rst("date2")) > 1 Then
      sOutput = CStr(datediff("y",rst("date1"), rst("date2"))) & " years "
   Else
      sOutput = CStr(datediff("y",rst("date1"), rst("date2"))) & " year "
   End IF
End IF
If datediff("m",rst("date1"), rst("date2")) > 0 Then
       sOutput = sOutput & CStr(datediff("m",rst("date1"), rst("date2"))) & " months "
   Else
      sOutput = CStr(datediff("m",rst("date1"), rst("date2"))) & " month "
   End IF
End If
If datediff("d",rst("date1"), rst("date2")) > 0 Then
       sOutput = sOutput & CStr(datediff("d",rst("date1"), rst("date2"))) & " days"
   Else
      sOutput = CStr(datediff("d",rst("date1"), rst("date2"))) & " day"
   End IF
End If

0
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 12328765
Oops, I made a mistake - the "Else ..." sOutput lines for the days and months wouldn't work right - here's the corrected version:

If datediff("y",rst("date1"), rst("date2")) > 0 Then
   If datediff("y",rst("date1"), rst("date2")) > 1 Then
      sOutput = CStr(datediff("y",rst("date1"), rst("date2"))) & " years "
   Else
      sOutput = CStr(datediff("y",rst("date1"), rst("date2"))) & " year "
   End IF
End IF
If datediff("m",rst("date1"), rst("date2")) > 0 Then
       sOutput = sOutput & CStr(datediff("m",rst("date1"), rst("date2"))) & " months "
   Else
      sOutput = sOutput & CStr(datediff("m",rst("date1"), rst("date2"))) & " month "
   End IF
End If
If datediff("d",rst("date1"), rst("date2")) > 0 Then
       sOutput = sOutput & CStr(datediff("d",rst("date1"), rst("date2"))) & " days"
   Else
      sOutput = sOutput & CStr(datediff("d",rst("date1"), rst("date2"))) & " day"
   End IF
End If


Incidentally, the resulting string would be in the format you requested.  Note: you could probably turn this into a function to reuse it easier later.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 12328886
I tried but I get the following error;

Error Type:
Microsoft VBScript compilation (0x800A0400)
Expected statement
/test/AdvRepResult.asp, line 114
End If

could u pls. check the synatax. some 'ifs' are there without 'end ifs'

ayha
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 150 total points
ID: 12329264
I wrote some code to show that difference between a given date and Now()--with a little modification, it might work for you:

function timeDifference(datDate)
     intDays = DateDiff("d",Now(), datDate)
     if intDays >0 then
          if Hour(datDate) > Hour(Now()) then
               datDate = DateAdd("h",- intDays*24, datDate)
          else
               intDays = intDays -1
               datDate = DateAdd("h",-intDays*24, datDate)
          end if
     end if
     intHours = DateDiff("h",Now(),datDate)
     if intHours >0 then
          if Minute(datDate) > Minute(Now()) then
               datDate = DateAdd("n",- intHours*60, datDate)
          else
               intHours = intHours -1
               datDate = DateAdd("n",- intHours*60, datDate)
          end if
     end if
     intMinutes = DateDiff("n",Now(),datDate)

     if intMinutes >0 then
          if Second(datDate) > Second(Now()) then
               datDate = DateAdd("s",- intMinutes*60, datDate)
          else
               intMinutes = intMinutes -1
               datDate = DateAdd("s",- intMinutes*60, datDate)
          end if
     end if
     intSeconds = DateDiff("s",Now(),datDate)

     timeDifference = " Days: " & intDays &  " Hours: " & intHours & " Minutes: " &  intMinutes  & " Seconds: " & intSeconds
end function
0
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 12331253
If datediff("y",rst("date1"), rst("date2")) > 0 Then
   If datediff("y",rst("date1"), rst("date2")) > 1 Then
      sOutput = CStr(datediff("y",rst("date1"), rst("date2"))) & " years "
   Else
      sOutput = CStr(datediff("y",rst("date1"), rst("date2"))) & " year "
   End IF
End IF
If datediff("m",rst("date1"), rst("date2")) > 0 Then
   if datediff("m",rst("date1"), rst("date2")) = 1 Then
       sOutput = sOutput & CStr(datediff("m",rst("date1"), rst("date2"))) & " month "
   Else
      sOutput = sOutput & CStr(datediff("m",rst("date1"), rst("date2"))) & " months "
   End IF
End If
If datediff("d",rst("date1"), rst("date2")) > 0 Then
   If datediff("d", rst("date1"), rst("date2")) = 1 Then
       sOutput = sOutput & CStr(datediff("d",rst("date1"), rst("date2"))) & " day"
   Else
      sOutput = sOutput & CStr(datediff("d",rst("date1"), rst("date2"))) & " days"
   End IF
End If



Sorry about that - try the above.  (My server's down at the moment and I can't test things out, but I think I straightened out the bugs).
0
 
LVL 7

Author Comment

by:ayha1999
ID: 12379039
@ leew,

I tried your latest code but it gives wrong result;

e.g. I have two date as follows;

5/9/2004  - 5/29/2004 and the result is;

20 years 20 days

could u pls check what's wrong.

@ FTB,

I have not checked ur post so far,I will chekc it later and get back to u.

ayha
0
 
LVL 96

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 100 total points
ID: 12382550
Sorry about that - debugged it in Visual Basic - SHOULD work with ASP:

    StartDate = rst("date1")
    EndDate = rst("date2")
    If DateDiff("yyyy", StartDate, EndDate) > 0 Then
       If DateDiff("yyyy", StartDate, EndDate) > 1 Then
          sOutput = CStr(DateDiff("yyyy", StartDate, EndDate)) & " years "
       Else
          sOutput = CStr(DateDiff("yyyy", StartDate, EndDate)) & " year "
       End If
    End If
   
    NewDate = DateAdd("yyyy", DateDiff("yyyy", StartDate, EndDate), StartDate)
    If DateDiff("m", NewDate, EndDate) > 0 Then
       If DateDiff("m", NewDate, EndDate) = 1 Then
            sOutput = sOutput & CStr(DateDiff("m", NewDate, EndDate)) & " month "
       Else
            sOutput = sOutput & CStr(DateDiff("m", NewDate, EndDate)) & " months "
       End If
    End If
    NewDate = DateAdd("m", DateDiff("m", StartDate, EndDate), StartDate)
    If DateDiff("d", NewDate, EndDate) > 0 Then
       If DateDiff("d", NewDate, EndDate) = 1 Then
           sOutput = sOutput & CStr(DateDiff("d", NewDate, EndDate)) & " day"
       Else
          sOutput = sOutput & CStr(DateDiff("d", NewDate, EndDate)) & " days"
       End If
    End If
0
 
LVL 7

Author Comment

by:ayha1999
ID: 12388276
@ leew,

I tried ur latest  but see the result;

I have the following dates;

5/9/2004    5/29/2004

the result is
4 years 5 months 3 days20 days

pls check it?

ayha
0
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 12388918
I should have my server systems running by the end of the weekend.  I'll check it then and let you know.
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12407793
> 4 years 5 months 3 days20 days

The trailing 20 days part seems correct... and it looks tacked onto the end of another value... so just to check... make sure you're not calling leew's code multiple times in the same page and concatenating the results...
0
 
LVL 7

Author Comment

by:ayha1999
ID: 12457222
thanks for all
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12457325
Glad to have helped,

FtB
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question