|
-
Mar 23rd, 2004, 12:21 PM
#1
Thread Starter
Addicted Member
better way to convert seconds into dd:hh:mm:ss?
right now i have
Code:
'Start time conversion
If Secs > 60 Then
Do
Secs = Secs - 60
Mins = Mins + 1
Loop Until Secs < 60
End If
If Mins > 60 Then
Do
Mins = Mins - 60
Hours = Hours + 1
Loop Until Mins < 60
End If
If Hours > 24 Then
Do
Hours = Hours - 24
Days = Days + 1
Loop Until Hours < 24
End If
and i don't think it's really efficient since this will be calculated every second.
in Secs, i have a huge number, which is the difference in time between today and some time after. i'd like to convert that into "dd Days hh Hours mm Minutes ss Seconds" without being as inefficient as what i have above. i could just format it as "dd:hh:mm:ss" but i'd like to have "Days Hours Minutes, and Seconds" though.
[vbcode]
Me.Hide()
[/vbcode]
-
Mar 23rd, 2004, 01:06 PM
#2
Frenzied Member
VB Code:
Try
Dim seconds As Integer
Dim d As Integer
Dim h As Integer
Dim m As Integer
Dim s As Integer
seconds = 86401 'one day, zero hours and mins and one sec
If (seconds >= 86400) Then
d = seconds \ 86400
seconds = seconds Mod 86400
End If
If (seconds >= 3600) Then
h = seconds \ 3600
seconds = seconds Mod 3600
End If
If (seconds >= 60) Then
m = seconds \ 60
seconds = seconds Mod 60
End If
s = seconds
Console.WriteLine("days " & "hours " & "mins " & "secs")
Console.WriteLine(d & " " & h & " " & m & " " & s)
Catch ex As Exception
End Try
is that what you need?
-
Mar 23rd, 2004, 01:17 PM
#3
Thread Starter
Addicted Member
hmm.. almost. thanks.
i read about the operater \ but forgot. what does that do?
EDIT:
i was confused. that was _exactly_ what i needed.
still, i don't know what \ does...
[vbcode]
Me.Hide()
[/vbcode]
-
Mar 23rd, 2004, 01:25 PM
#4
Frenzied Member
This is from MSDN
\ operator
Divides two numbers and returns an integer result.
number1 \ number2
Parts
number1
Required. Any numeric expression of an integral type.
number2
Required. Any numeric expression of an integral type.
Supported Types
Byte, Short, Integer, or Long.
Result
The result is the integer quotient of number1 and number2, dropping the remainder.
Remarks
Before division is performed, any floating-point numeric expressions are coerced to Long if Option Strict is Off. If Option Strict is On, a compiler error results.
The data type of the result is Byte, Short, Integer, or Long. Any fractional portion is truncated.
If any expression is stated as Nothing, it is treated as zero. Attempting to perform integer division by zero causes a DivideByZeroExeception to be thrown.
Example
This example uses the \ operator to perform integer division. The result is an integer representing the integer quotient of the two operands.
Dim myValue As Integer
myValue = 11 \ 4 ' Returns 2.
myValue = 9 \ 3 ' Returns 3.
myValue = 100 \ 3 ' Returns 33.
MyValue = 67 \ -3 ' Returns -22.
but, the short answer is: Divides two numbers and returns an integer result.
-
Mar 24th, 2004, 12:00 AM
#5
VB Code:
MsgBox(TimeSpan.FromSeconds(6000).ToString)
Days will only be shown if there are any.
-
Mar 24th, 2004, 01:03 AM
#6
Thread Starter
Addicted Member
lol... that makes me laugh.
so simple. yet so difficult when you don't know about it.
final version:
VB Code:
Dim Seconds As Integer = DateDiff(DateInterval.Second, Now, dtpDue.Value)
Dim d As Integer = TimeSpan.FromSeconds(Seconds).Days
Dim h As Integer = TimeSpan.FromSeconds(Seconds).Hours
Dim m As Integer = TimeSpan.FromSeconds(Seconds).Minutes
Dim s As Integer = TimeSpan.FromSeconds(Seconds).Seconds
MsgBox(d & " Days " & h & " Hours " & m & " Minutes " & s & " Seconds")
thanks again, guys.
[vbcode]
Me.Hide()
[/vbcode]
-
Mar 24th, 2004, 02:58 PM
#7
Frenzied Member
awesome. I didn't knw about that either!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|