Link to home
Start Free TrialLog in
Avatar of Bob Schneider
Bob SchneiderFlag for United States of America

asked on

Convert Seconds to Minutes in Classic ASP

I need to convert seconds, out to the thousandths, to hh:mm:ss.xxx format.  I have a script that comes close but I believe it chops off the decimal seconds.  Can someone give me an alternative or show how to modify this one pleaes?

Function SecondsToTime(ByVal sglScnds)
  Dim hours, minutes, seconds

  ' calculates whole hours (like a div operator)
  hours = sglScnds \ 3600

  ' calculates the remaining number of seconds
  intSeconds = sglScnds Mod 3600

  ' calculates the whole number of minutes in the remaining number of seconds
  minutes = sglScnds \ 60

  ' calculates the remaining number of seconds after taking the number of minutes
  seconds = sglScnds Mod 60

  ' returns as a string
  SecondsToTime = hours & ":" & minutes & ":" & seconds
End Function

Open in new window


Thank You!
SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or you can use

dim ms = Split(CStr(sglScnds), ".")(1)
SecondsToTime = hours & ":" & minutes & ":" & seconds & ":" & left("000" & ms, 3)
Avatar of Big Monty
check out this question on EE that does something very similar. All you need to do is take out the check to add/subtract time:

<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<TITLE>ASP Test Page</TITLE>
<%
Function GetCurrentDateAndTimeMinus1Hour()
	Dim timeStamp
        Dim dateStamp
	timeStamp = Timer
	dateStamp = Now()
	
	Dim dateAndTime
	'year
	dateAndTime = dateAndTime & DatePart("yyyy",dateStamp)
	dateAndTime = dateAndTime & "-"
	'month
        dateAndTime = dateAndTime & right("0" & DatePart("m",dateStamp),2)
	dateAndTime = dateAndTime & "-"
	'day
        dateAndTime = dateAndTime & right("0" & DatePart("d",dateStamp),2)
	dateAndTime = dateAndTime & " "	
        'hour
        dateAndTime = dateAndTime & right("0" & Fix(timeStamp / 3600),2)
	dateAndTime = dateAndTime & ":"
        'minute
        timeStamp = timeStamp - (Fix(timeStamp / 3600)*3600)
        dateAndTime = dateAndTime & right("0" & Fix(timeStamp / 60),2)
	dateAndTime = dateAndTime & ":"
        'second
        timeStamp = timeStamp - (Fix(timeStamp / 60)*60)
        dateAndTime = dateAndTime & right("0" & Fix(timeStamp),2)
	dateAndTime = dateAndTime & "."
        'millisecond
        timeStamp = Fix((timeStamp - Fix(timeStamp))*1000)
        dateAndTime = dateAndTime  & right("00" & timeStamp ,3)
	
	GetCurrentDateAndTimeMinus1Hour = dateAndTime
End Function
%> 

</HEAD>

<BODY>
<%=GetCurrentDateAndTimeMinus1Hour %>
</BODY>
</HTML>

Open in new window


just a heads up, this will result in an error:

dim ms = Split(CStr(sglScnds), ".")(1)

it should be:

dim ms : ms = Split(CStr(sglScnds), ".")(1)