claracruz
asked on
Duration in days, hours, minutes and seconds
Hello Experts,
how would I change this;-
result.Days.ToString()
to give me the result iin days, hours, minutes and seconds.
Also, how would I go about setting this duration to a countdown timer.
how would I change this;-
result.Days.ToString()
to give me the result iin days, hours, minutes and seconds.
Also, how would I go about setting this duration to a countdown timer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The first response won't work as it will give you a total of the time difference in each of the specified units. I take it the result needs to be broken down into the maximum amount of days then the remainder used to calculate hours etc.
Below is a ASP function I once made which could be easily converted to .NET, I've not had need to do this in ASP.NET so am not aware if there is a new feature that would accomodate your needs better. Here is a link I have used to adapt a javascript count down timer in the past, should set you on the right track. http://www.hashemian.com/tools/javascript-countdown.htm.
function timeDifference(date1,date2 )
'-- Calculates the difference in minutes, hours, days, weeks and years
'-- between two dates.
if isDate(date1) AND isDate(date2) then
tUnit = dateDiff("s",date1,date2)
'-- Divide seconds into minutes
tSecond = tUnit / 60
' -- Discard remainder and assign as minute
tMinute = fix(tSecond)
' -- Retreive decimal remainder and convert to seconds
tSecond = round((tSecond - tMinute)*60)
'-- Divide minutes into hours
tMinute = tMinute / 60
tHour = fix(tMinute)
tMinute = round((tMinute - tHour)*60)
'-- Divide hours into days
tHour = tHour/24
tDay = fix(tHour)
tHour = round((tHour-tDay)*24)
'-- Divide days into weeks
tDay = tDay/7
tWeek = fix(tDay)
tDay = round((tDay-tWeek)*7)
'-- Divide weeks into years
tWeek = tWeek/52
tYear = fix(tWeek)
tWeek = round((tWeek-tYear)*52)
if tYear<>0 then
tWrite=tWrite&", "&tYear&" Year"
if tYear>1 then tWrite=tWrite&"s"
end if
if tWeek<>0 then
tWrite=tWrite&", "&tWeek&" Week"
if tWeek>1 then tWrite=tWrite&"s"
end if
if tDay<>0 then
tWrite=tWrite&", "&tDay&" Day"
if tDay>1 then tWrite=tWrite&"s"
end if
if tHour<>0 then
tWrite=tWrite&", "&tHour&" Hour"
if tHour>1 then tWrite=tWrite&"s"
end if
if tMinute<>0 then
tWrite=tWrite&", "&tMinute&" Minute"
if tMinute>1 then tWrite=tWrite&"s"
end if
if tSecond<>0 AND tHour=0 AND tDay=0 AND tWeek=0 AND tYear=0 then
tWrite=tWrite&", "&tSecond&" Second"
if tSecond>1 then tWrite=tWrite&"s"
end if
if tWrite <> "" then
timeDifference = right(tWrite,len(tWrite)-2 )
end if
end if
end function
Below is a ASP function I once made which could be easily converted to .NET, I've not had need to do this in ASP.NET so am not aware if there is a new feature that would accomodate your needs better. Here is a link I have used to adapt a javascript count down timer in the past, should set you on the right track. http://www.hashemian.com/tools/javascript-countdown.htm.
function timeDifference(date1,date2
'-- Calculates the difference in minutes, hours, days, weeks and years
'-- between two dates.
if isDate(date1) AND isDate(date2) then
tUnit = dateDiff("s",date1,date2)
'-- Divide seconds into minutes
tSecond = tUnit / 60
' -- Discard remainder and assign as minute
tMinute = fix(tSecond)
' -- Retreive decimal remainder and convert to seconds
tSecond = round((tSecond - tMinute)*60)
'-- Divide minutes into hours
tMinute = tMinute / 60
tHour = fix(tMinute)
tMinute = round((tMinute - tHour)*60)
'-- Divide hours into days
tHour = tHour/24
tDay = fix(tHour)
tHour = round((tHour-tDay)*24)
'-- Divide days into weeks
tDay = tDay/7
tWeek = fix(tDay)
tDay = round((tDay-tWeek)*7)
'-- Divide weeks into years
tWeek = tWeek/52
tYear = fix(tWeek)
tWeek = round((tWeek-tYear)*52)
if tYear<>0 then
tWrite=tWrite&", "&tYear&" Year"
if tYear>1 then tWrite=tWrite&"s"
end if
if tWeek<>0 then
tWrite=tWrite&", "&tWeek&" Week"
if tWeek>1 then tWrite=tWrite&"s"
end if
if tDay<>0 then
tWrite=tWrite&", "&tDay&" Day"
if tDay>1 then tWrite=tWrite&"s"
end if
if tHour<>0 then
tWrite=tWrite&", "&tHour&" Hour"
if tHour>1 then tWrite=tWrite&"s"
end if
if tMinute<>0 then
tWrite=tWrite&", "&tMinute&" Minute"
if tMinute>1 then tWrite=tWrite&"s"
end if
if tSecond<>0 AND tHour=0 AND tDay=0 AND tWeek=0 AND tYear=0 then
tWrite=tWrite&", "&tSecond&" Second"
if tSecond>1 then tWrite=tWrite&"s"
end if
if tWrite <> "" then
timeDifference = right(tWrite,len(tWrite)-2
end if
end if
end function
If result is a TimeSpan
.Days, .Hours, .Minutes and .Seconds represent the partials
(note that .TotalDays, .TotalHours, .TotalMinutes and .TotalSeconds represent the whole timespan expressed by these demoninations)
If result is a DateTime
result.ToString("d hh mm ss") will return the values as requested.
Stephen
.Days, .Hours, .Minutes and .Seconds represent the partials
(note that .TotalDays, .TotalHours, .TotalMinutes and .TotalSeconds represent the whole timespan expressed by these demoninations)
If result is a DateTime
result.ToString("d hh mm ss") will return the values as requested.
Stephen
ASKER
how about when binding data to a control like so- <%# Eval("STARTTIME")%>
http://msdn2.microsoft.com/en-us/library/system.timespan.aspx