Print Page | Close Window

Time formatting

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=21000
Printed Date: 29 March 2026 at 4:17pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Time formatting
Posted By: PuNkY
Subject: Time formatting
Date Posted: 12 August 2006 at 7:46am

Hello everyone,
I need a little help. I’m trying to make a script that requires me to show the user how many years/months/weeks/hours/minutes the posted something (like Posted: 2 days 1 hour 3 minutes ago). I have made an attempt but failed. When the post becomes a month old it says something like "32 days" instead of 1 month and also when the post becomes a year old it says "1 year 365 days". Maybe one of you can help me with my script.


<%
Function Time(post_time)
Dim strNow
Dim H
Dim M
Dim Y
Dim D

strNow = Now()

H = datediff("h",post_time,strNow)
M = datediff("n",post_time,strNow)
Y = datediff("yyyy",post_time,strNow)

D = int(h / 24) :if d > 0 then d = d & " days " :do while h >= 24 :h = h - 24 :loop :else d = ""

Do While M >= 60
    M = M - 60
Loop
Do While S >= 60
    S = S - 60
Loop


If H > 0 Then
    H = H & " hours "
Else
    H = ""
End If

If M > 0 Then
    M = M & "minutes "
Else
    M = ""
End If

If Y > 0 Then
    Y = Y & " years "
Else
    Y = ""
End If

Response.Write y & d & h & m & s
End Function
%>




Replies:
Posted By: michael
Date Posted: 13 August 2006 at 3:07pm
You need to substact the number of years * 12 from the month which in the first year will be 0 (0*12=0),same for the month, you need to substract the days

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: PuNkY
Date Posted: 14 August 2006 at 5:11am
Originally posted by michael michael wrote:

You need to substact the number of years * 12 from the month which in the first year will be 0 (0*12=0),same for the month, you need to substract the days
Can you give me an example. I dont FULLY understand.


Posted By: michael
Date Posted: 14 August 2006 at 6:46pm

Sub sTime(post_time)

Dim y,m,d,h

y= Datepart("yyyy",now()) - Datepart("yyyy",post_time)
m= Datepart("m",now()) - Datepart("m",post_time)
d = Datepart("d",now()) - Datepart("d",post_time)
h = Datepart("h",now()) - Datepart("h",post_time)

If m < 0 THEN m=m+daysInMonth   'there are plenty daysInMonth funcitons on the web
If d < 0 ...... 'same thing here

Response.Write (y & " years " & m & " Month " & d & " days " & h & " hours since ")


End Sub
%>


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net