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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting days, hours, minuts from date and now() 2

Status
Not open for further replies.

snowboardr

Programmer
Feb 22, 2002
1,401
PH
My code seems to be buggy... i have no idea why its not returning the correct time ... its returning approx 17 hours... when the dats are 8/5 and 8/13


Code:
<%
Function GetTime(aFirstDate,aSecondDate)
		 
		  Dim t_min, t_hour, t_day
		 Dim GetTimeTmp
		 Dim sColor

		 
		If isDate(aFirstDate) then
		  t_min = DateDiff("n",aFirstDate, aSecondDate)

		   t_hour = Fix(t_min/60)
		   t_min = t_min mod 60
		   
		   t_day = Fix(t_hour/24)
		   t_hour = t_hour mod 24

				  
		   If t_hour > 0 Then  GetTimeTmp = GetTimeTmp & t_hour & " hr"
				If t_hour > 1 then 
					GetTimeTmp = GetTimeTmp &  "s "
						Else 
					 GetTimeTmp  = GetTimeTmp & " "
				End If
		
		   If t_min > 0 Then GetTimeTmp = GetTimeTmp & t_min & " min"
				If t_min > 1 then 
					GetTimeTmp = GetTimeTmp &  "s "
						Else 
					 GetTimeTmp  = GetTimeTmp & " "
				End If
				
		   If Len(GetTimeTmp) > 0 Then GetTimeTmp = Left(GetTimeTmp,Len(GetTimeTmp)-1)
			
			GetTimeTmp = GetTimeTmp & " ago"
		
			If trim(GetTimeTmp) = "ago" then
				GetTimeTmp = "Less than 1 minute..."
				scolor = "newpost"
			Else
				If instr(GetTimeTmp,"hrs") = 0 then
					scolor = "newpost"
				Else
					scolor = "q_values"
				End iF
			End If

		GetTime = "<span class='" & sColor & "'>" & GetTimeTmp & "</span>"
		
End If
End Function

Army : Combat Engineer : 21B

 
Instead of Datediff, just subtract the 2 dates. The integer part of the answer will be the number of days, and the fraction is fractional days. It's easy to process that into hours and minutes using Int.
Difference will be (date1 - date2)
Days will be Int(Difference)
Hours will be 24 * (Difference-Int(Difference))
.
.
etc

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
That didn't work it returns nothing..

Code:
<%
Function fixdate(date1,date2)
	Dim tmpdiff
	date1 = cdate(date1)
	date2 = cdate(date2)
	tmpdiff = (date1 - date2)
	
	fixdate = tmpdate
End Function

Response.write fixdate("08/27/1983",date())

%>

Army : Combat Engineer : 21B

 
Ok, you had me for a bit to. I kept looking for a bad calculation and the problem isn't something that is there, it's something that isn't.
You don't have an output section for days :)

I had to go through the code 3 times, then switch to the windows box and try it myself before I noticed it. Add in a section to add days to the output and you should be good.


Suggestions: I like doing fuzzy date spans. For instance, if it's more than a certain number of days, don't bother adding hours. If it's more than a certain number of hours, don't bother with minutes. If it's more than a certain numer of days, switch to months or years, and so on.

Something like:
Code:
Function IIF(condition,trueCase,falseCase)
	IIF = falseCase
	If condition Then IIF = trueCase
End Function

Function GetTime(aFirstDate,aSecondDate)
	Dim t_min, t_hour, t_day, t_mnth, t_year, t_part, is_neg

	If IsDate(aFirstDate) And IsDate(aSecondDate) Then
		'get the days to determine how exact to make the date
		t_min = DateDiff("n",aFirstDate,aSecondDate)
		If t_min < 0 Then is_neg = True
		t_min = Abs(t_min)
		
		t_hour = Fix(t_min/60)
		t_min = t_min Mod 60
		
		t_day = Fix(t_hour/24)
		t_hour = t_hour Mod 60

		'use select hack to do range checks
		Select Case True
			Case t_day < 2
				'display minutes and hours
				If t_min > 0 Or t_hour > 0 Then
					If t_hour > 0 Then GetTime = t_hour & IIF(t_hour > 1," hours "," hour ")
					If t_min > 0 Then GetTime = GetTime & t_min & IIF(t_min > 1," minutes "," minute ")
				Else
					GetTime = GetTime & "Less than a minute"
				End If
			Case t_day < 60
				'display days
				GetTime = "About " & t_day & " days "
			Case t_day < 740
				'display year and month
				t_mnth = Fix(t_day/30.8)
				t_year = Fix(t_mnth/12)
				t_mnth = t_mnth Mod 12
				
				GetTime = "About "
				If t_mnth > 0 Then GetTime = GetTime & t_mnth & IIF(t_mnth > 1," months "," month ")
				If t_year > 0 Then GetTime = GetTime & t_year & IIF(t_year > 1," years "," year ")
			Case Else
				'display year
				t_mnth = Fix(t_day/30.8)
				t_year = Fix(t_mnth/12)
				t_mnth = t_mnth Mod 12
				If t_mnth < 2 Then
					t_part = ""
				ElseIf t_mnth < 5 Then
					t_part = " and a quarter"
				ElseIf t_mnth < 8 Then
					t_part = " and a half"
				ElseIf t_mnth < 11 Then
					t_part = " and three quarters"
				Else
					t_year = t_year + 1
				End If

				GetTime = "About " & t_year & t_part & " years"
		End Select

		'do the negative
		If is_neg Then GetTime = GetTime & " ago"
	End If
End Function

Obviously this won't quite fit your needs, since I didn't include the css logic. I feel tht it would be better to not include that logic in this code, since you can then re-use this code for other thngs besides post age. Additionally I made this work for future dates rather than just age dates, so depending on the order fo the date you give it, you may or may not get an "ago".

I wrote this on the fly, haven't tested it, and it probably has at least a couple mis-types in it. Plus it's not as short as john's proposed method. I included it in the hopes that it will give you an idea or two, if not for this time then maybe for next time :)

 
Thanks Tarwn!! Believe it or not... i ran it in a .vbs script and it worked without any changes... haha

All i did was add your above code to a text file and saved it as "date.vbs" with quotes so it kept the .vbs... and added this to it:

Code:
msgbox GetTime(cdate("08/27/1983"),date())


Well done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top