Convert seconds to days, hours, minutes and seconds.

  • Thread starter Thread starter Sheldon
  • Start date Start date
S

Sheldon

Hello -

I have about ten integer values that each need to be converted to days,
hours, minutes and seconds. I can't seem to find a formula for same that
works. These integer values come from a Sql Server database that SUMS up the
seconds. The only way SUM will work in SS is with an integer value,
therefore, I could not use datetime in Sql Server.)

I need to ultimately put the converted values into ten datetimepicker
controls.

Does anyone have code for doing this? Any help will be appreciated!
 
Sheldon said:
Hello -

I have about ten integer values that each need to be converted to
days, hours, minutes and seconds. I can't seem to find a formula
for same that works. These integer values come from a Sql Server
database that SUMS up the seconds. The only way SUM will work in SS
is with an integer value, therefore, I could not use datetime in Sql
Server.)

I need to ultimately put the converted values into ten
datetimepicker controls.

Does anyone have code for doing this? Any help will be appreciated!


Use the TimeSpan structure, for example

Dim ts = TimeSpan.FromSeconds(1234)

Then you can access it's members, ts.days, ts.hours etc.


Armin
 
Hello -

I have about ten integer values that each need to be converted to days,
hours, minutes and seconds.  I can't seem to find a formula for same that
works.  These integer values come from a Sql Server database that SUMS up the
seconds.  The only way SUM will work in SS is with an integer value,
therefore, I could not use datetime in Sql Server.)

I need to ultimately put the converted values into ten datetimepicker
controls.

Does anyone have code for doing this?  Any help will be appreciated!

Sounds like a good job for some simple looping and straight division.
You should be able to figure it out once you start playing with some
code.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Sheldon said:
Hello -

I have about ten integer values that each need to be converted to days,
hours, minutes and seconds. I can't seem to find a formula for same that
works. These integer values come from a Sql Server database that SUMS up
the
seconds. The only way SUM will work in SS is with an integer value,
therefore, I could not use datetime in Sql Server.)

I need to ultimately put the converted values into ten datetimepicker
controls.

Does anyone have code for doing this? Any help will be appreciated!

There are many ways to do this, but can you find this example useful:

Dim Seconds As Integer = 500
Dim Example1 As New TimeSpan(0, 0, Seconds)
Dim Example2 As DateTime
Example2 = Example2.AddSeconds(Seconds)

MsgBox(Seconds & " seconds is " & Example1.TotalHours & " hours")
MsgBox(Seconds & " seconds is " & Example2.Minute & " minutes and "
_
& Example2.Second & " seconds")

-Teemu
 
Sheldon said:
Hello -

I have about ten integer values that each need to be converted to days,
hours, minutes and seconds. I can't seem to find a formula for same that
works. These integer values come from a Sql Server database that SUMS up
the
seconds. The only way SUM will work in SS is with an integer value,
therefore, I could not use datetime in Sql Server.)

I need to ultimately put the converted values into ten datetimepicker
controls.

Does anyone have code for doing this? Any help will be appreciated!


'The number of seconds in a day is (24 * 60 * 60 )
'Assuming TotalSeconds is your variable
'use integer division
Dim Daycount as Integer = TotalSeconds \ (24 * 60 * 60)
Dim SecondsRemaining as Integer = TotalSeconds - (DayCount * (24 * 60 *
60) )
Dim HourCount as Integer = SecondsRemaining \ (60 * 60)
SecondsRemaining = SecondsRemaining Mod (60 * 60)
Dim MinutesCount as Integer = SecondsRemaining \ 60
Dim SecondsCount as Integer = SecondsRemaining Mod 60
 
Sheldon said:
Hello -

I have about ten integer values that each need to be converted to days,
hours, minutes and seconds. I can't seem to find a formula for same that
works. These integer values come from a Sql Server database that SUMS up the
seconds. The only way SUM will work in SS is with an integer value,
therefore, I could not use datetime in Sql Server.)

I need to ultimately put the converted values into ten datetimepicker
controls.

Does anyone have code for doing this? Any help will be appreciated!

The most straight forward method is to simply turn the seconds into a
TimeSpan value:

t AS TimeSpan = TimeSpan.FromSeconds(seconds)

If you need a DateTime value, you have to pick a date that you start
counting from (i.e. the date that a zero seconds value would represent).
Then you just add the seconds to that.

d As DateTime = new DateTime(2008,1,1).AddSeconds(seconds)
 
Back
Top