Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateDiff Help 1

Status
Not open for further replies.

snowboardr

Programmer
Feb 22, 2002
1,401
PH
Hi, I am trying to display the difference in two dates using Datediff. Its for a forum script I am working on. However I can't seem to get it output right:

25HRS. 1507 mins ago
is what i am getting... I know my code is ugly, but i Just need to be pointed in the right direction on how to get the correct hrs and mins to show up then ill clean it up..

Thanks, for any help.
Jason

Code:
'# DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]])

sDateTimeNow = Now()
sReplyDay = DateDiff("d",sReplyTime,sDateTimeNow)
sReplyHour = DateDiff("h",sReplyTime,sDateTimeNow)
sReplyMinute = DateDiff("n",sReplyTime,sDateTimeNow)

sReplyColor = "mt"

If sReplyDay < 2 then
			If sReplyHour = 1 then	
				sReplyTimeLong = sReplyHour & "+ HRs. ago"
					Else
					If sReplyHour = 0 then	
										If sReplyMinute = "0" then
												sReplyTimeLong = "NEW"
												sReplyColor = "npst"
													Else
												sReplyTimeLong = sReplyMinute & " mins ago"
												If sReplyMinute < 20 then
														 sReplyColor = "npst"
												End If
										End If
								Else
								sReplyTimeLong = sReplyHour & "HRS. " & sReplyMinute & "mins ago"
					End If
			End If
		Else
		sReplyTimeLong = sReplyTime
End If

www.sitesd.com
ASP WEB DEVELOPMENT
 
The problem is that DateDiff does the full difference in the factor you give it. So if I were to DateDiff between now (6:38) and an hour ago (5:38), I would get:
DateDiff in Hours: 1
DateDiff in Minutes: 60
DateDiff in seconds: 3600

What you need to do is get the DateDiff in the smallest factor you will use then convert upwards, something like:
Code:
Function GetTime(aFirstDate,aSecondDate)
   Dim t_min, t_hour, t_day
   'get the total minutes
   t_min = DateDiff("n",aFirstDate, aSecondDate)

   'get hours and remove that amount from minutes
   t_hour = Fix(t_min/60)
   t_min = t_min mod 60
   
   'get days and remove that amount from hours
   t_day = Fix(t_hour/24)
   t_hour = t_hour mod 24

   If t_day > 0 Then GetTime = t_day & "d "
   If t_hour > 0 Then GetTime = GetTime & t_hour & "h "
   If t_min > 0 Then GetTime = GetTime & t_min & "m "

   If Len(GetTime) > 0 Then GetTime = Left(GetTime,Len(GetTime)-1)
End Function

Technically I think I could get this all down to one line, but I figured spelling it out would prove more helpful :)

-T

barcode_1.gif
 
Thanks alot Tarwn

www.sitesd.com
ASP WEB DEVELOPMENT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top