By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

Convert seconds to days, hours, minutes and seconds.

P: n/a
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
Jul 5 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jul 5, 3:37*pm, Sheldon <Shel...@discussions.microsoft.comwrote:
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
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/
Jul 5 '08 #2

P: n/a

"Sheldon" <Sh*****@discussions.microsoft.comkirjoitti viestissä
news:5B**********************************@microsof t.com...
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

Jul 5 '08 #3

P: n/a

"Sheldon" <Sh*****@discussions.microsoft.comwrote in message
news:5B**********************************@microsof t.com...
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

'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


Jul 6 '08 #4

P: n/a
Sheldon wrote:
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)

--
Göran Andersson
_____
http://www.guffa.com
Jul 6 '08 #5

P: n/a
"Sheldon" <Sh*****@discussions.microsoft.comschrieb
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
Jul 6 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.