//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
      COM / ActiveX Components
      Forms
      General Topics
      Date/Time Routines
      Email Scripts & Info
      Files/Directories & FSO
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
classicasp.aspfaq.com

ASP FAQ Tutorials :: Classic ASP 1.0 :: Date/Time Routines :: How do I convert a timespan, in seconds, to HH:MM:SS?


How do I convert a timespan, in seconds, to HH:MM:SS?

Often you want to compare two dates and/or times and express the difference in HH:MM:SS. Except there is no decent way to express the relationship this way; the built-in DATEDIFF function returns a single integer (seconds, minutes, hours, days, months, years, etc). 
 
Here is a way in T-SQL to accomplish this: 
 
DECLARE @d1 DATETIME, @d2 DATETIME, @sd INT 
 
SET @d1 = '20050812 05:32:45' 
SET @d2 = '20050817 02:15:46' 
 
SET @sd = DATEDIFF(SECOND, @d1, @d2) 
 
SELECT [HH:MM:SS] =  
    CASE WHEN @sd/3600<10 THEN '0' ELSE '' END 
    + RTRIM(@sd/3600) 
    + ':' + RIGHT('0'+RTRIM((@sd % 3600) / 60),2) 
    + ':' + RIGHT('0'+RTRIM((@sd % 3600) % 60),2)
 
Here it is in VBScript: 
 
<% 
    Function TimeSpan(dt1, dt2) 
        If (isDate(dt1) And IsDate(dt2)) = false Then 
            TimeSpan = "00:00:00" 
            Exit Function 
        End If 
 
        seconds = Abs(DateDiff("S", dt1, dt2)) 
        minutes = seconds \ 60 
        hours = minutes \ 60 
        minutes = minutes mod 60 
        seconds = seconds mod 60 
 
        if len(hours) = 1 then hours = "0" & hours 
 
        TimeSpan = hours & ":" & _ 
            RIGHT("00" & minutes, 2) & ":" & _ 
            RIGHT("00" & seconds, 2) 
    End Function 
 
    d1 = "2002-03-27 9:20:25 AM" 
    d2 = "2002-03-27 9:20:45 AM" 
 
    Response.Write TimeSpan(d1, d2) 
%>

Related Articles

Can I get millisecond accuracy in ASP?
Can I make VBScript format dates for me?
Could I get a little help with dates?
Given a date, how do I find the beginning and end of that week?
Given two dates, how do I determine an age?
How do I calculate dates, such as the first day of the month?
How do I convert a DATEDIFF to days, hours, and minutes?
How do I convert local time to UTC (GMT) time?
How do I count the number of business days between two dates?
How do I delimit/format dates for database entry?
How do I determine the number of seconds since 1/1/1970?
How do I display time in military format?
How do I implement a calendar / datepicker in ASP?
How do I select time only from a DATETIME column?
Why do I have problems inserting NOW() into a database?
Why does JavaScript's document.lastModified() not work in ASP files?

 

 


Created: 3/27/2002 | Last Updated: 9/12/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (364)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...