Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / December 2005

Tip: Looking for answers? Try searching our database.

Convert seconds  to 00:00:00 (HH:MM:SS) format. Please help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Barrido - 27 Dec 2005 10:57 GMT
I have this for example:

Dim iSeconds as int32 = 3600      '3600 seconds  is one hour
How do i convert it to "01:00:00" ? Please help.

Thanks in advance!
Joergen Bech <jbech<NOSPAM>@ - 27 Dec 2005 11:12 GMT
Perhaps

       Dim iSeconds As Int32 = 3600
       Dim ts As New TimeSpan(0, 0, iSeconds)
       MsgBox(ts.ToString)

/JB

>I have this for example:
>
>Dim iSeconds as int32 = 3600      '3600 seconds  is one hour
>How do i convert it to "01:00:00" ? Please help.
>
>Thanks in advance!
Michael Barrido - 27 Dec 2005 11:25 GMT
Thank you very much!

> Perhaps
>
[quoted text clipped - 10 lines]
>>
>>Thanks in advance!
Vadym Stetsyak - 27 Dec 2005 11:16 GMT
TimeSpan span = TimeSpan.FromSeconds(3600);
string timeInterval = interval.ToString("HH:MM:SS");

Signature

Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

>I have this for example:
>
> Dim iSeconds as int32 = 3600      '3600 seconds  is one hour
> How do i convert it to "01:00:00" ? Please help.
>
> Thanks in advance!
Michael Barrido - 27 Dec 2005 11:24 GMT
Thank you very much!

> TimeSpan span = TimeSpan.FromSeconds(3600);
> string timeInterval = interval.ToString("HH:MM:SS");
[quoted text clipped - 5 lines]
>>
>> Thanks in advance!
Joergen Bech <jbech<NOSPAM>@ - 27 Dec 2005 11:59 GMT
>TimeSpan span = TimeSpan.FromSeconds(3600);
>string timeInterval = interval.ToString("HH:MM:SS");

That won't work. The ToString method of the TimeSpan
object does not take a format parameter, so it would
still be

       Dim span As TimeSpan = TimeSpan.FromSeconds(iSeconds)
       MsgBox(span.ToString())

But you are right: .FromSeconds is a more appropriate answer to
the original problem (though it looks as if FromSeconds requires
quite a bit more work to be performed under the covers). Ah, I'm
just nit-picking.

Have a look at the ToString function below (from Reflector).
A shorter, faster version could be written by hand to deal with
the specific requirement, but the formatting is the same as what
the original poster wanted:

---snip---
Public Overrides Function ToString() As String
     Dim builder1 As New StringBuilder
     Dim num1 As Integer = CType((Me._ticks / 864000000000), Integer)
     Dim num2 As Long = (Me._ticks Mod 864000000000)
     If (Me._ticks < 0) Then
           builder1.Append("-")
           num1 = -num1
           num2 = -num2
     End If
     If (num1 <> 0) Then
           builder1.Append(num1)
           builder1.Append(".")
     End If
     builder1.Append(Me.IntToString(CType(((num2 / 36000000000) Mod
CType(24, Long)), Integer), 2))
     builder1.Append(":")
     builder1.Append(Me.IntToString(CType(((num2 / CType(600000000,
Long)) Mod CType(60, Long)), Integer), 2))
     builder1.Append(":")
     builder1.Append(Me.IntToString(CType(((num2 / CType(10000000,
Long)) Mod CType(60, Long)), Integer), 2))
     Dim num3 As Integer = CType((num2 Mod CType(10000000, Long)),
Integer)
     If (num3 <> 0) Then
           builder1.Append(".")
           builder1.Append(Me.IntToString(num3, 7))
     End If
     Return builder1.ToString
End Function
---snip---

/JB
Herfried K. Wagner [MVP] - 27 Dec 2005 12:02 GMT
"Michael Barrido" <jmmrb@hotmail.com> schrieb:
> Dim iSeconds as int32 = 3600      '3600 seconds  is one hour
> How do i convert it to "01:00:00" ? Please help.

\\\
MsgBox((New Date).AddSeconds(3600).ToString("hh:mm:ss"))
///

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>

james - 27 Dec 2005 13:22 GMT
> "Michael Barrido" <jmmrb@hotmail.com> schrieb:
>> Dim iSeconds as int32 = 3600      '3600 seconds  is one hour
[quoted text clipped - 3 lines]
> MsgBox((New Date).AddSeconds(3600).ToString("hh:mm:ss"))
> ///

For a bit more detail ( AM/PM) do this:

\\\
MsgBox((New Date).AddSeconds(3600).ToString("hh:mm:ss tt"))
///

I had been trying to get the same results as the original poster and also wanted AM/PM
and remembered a post by Chris Dunaway not too long ago helping me with the same
problem. So, I thought I would add the "tt" to help.
james
Joergen Bech <jbech<NOSPAM>@ - 27 Dec 2005 14:04 GMT
>> "Michael Barrido" <jmmrb@hotmail.com> schrieb:
>>> Dim iSeconds as int32 = 3600      '3600 seconds  is one hour
[quoted text clipped - 14 lines]
>problem. So, I thought I would add the "tt" to help.
>james

No, that is not quite enough:

The "tt"-specifier depends on the current culture. On my
machine (English XP, but with default Danish regional settings),
there are no AM/PM symbols, so the suggested "hh:mm:ss tt"
would not provide complete time information on my system.
I would just see something like "11:59:00 " (yes, including the
trailing space).

At the very least, make sure that the requirements for using
"hh:mm:ss tt" are fulfilled, i.e. that AM/PM designators have
been specified for the current culture.

For completeness sake:

hh = 12-hour format (1-12).

HH = 24-hour format (0-23).

Be careful not to write MM (month) instead of mm (minute).

For more information, see "Custom DateTime Format Strings"
in the documentation.

Regards,

Joergen Bech
james - 27 Dec 2005 16:26 GMT
"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAM> wrote in message news:lih2r151ircsc1fbe0ejbhjb99gtouctjm@4ax.com...

>>For a bit more detail ( AM/PM) do this:
>>
[quoted text clipped - 34 lines]
>
> Joergen Bech

Thank you Joergen. I have read up on the "Custom DateTime Format Strings", since Chris had given me
the help that I mentioned in my post.
Next time I'll leave my 2 cents in my pocket.
james
Herfried K. Wagner [MVP] - 27 Dec 2005 14:27 GMT
"james" <jjames700ReMoVeMe at earthlink dot net> schrieb:
>>> Dim iSeconds as int32 = 3600      '3600 seconds  is one hour
>>> How do i convert it to "01:00:00" ? Please help.
[quoted text clipped - 8 lines]
> MsgBox((New Date).AddSeconds(3600).ToString("hh:mm:ss tt"))
> ///

Well, I would not do that if I would want to display the size (quantity) of
a time span.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>

james - 27 Dec 2005 16:34 GMT
> "james" <jjames700ReMoVeMe at earthlink dot net> schrieb:
>>>> Dim iSeconds as int32 = 3600      '3600 seconds  is one hour
[quoted text clipped - 11 lines]
>
> Well, I would not do that if I would want to display the size (quantity) of a time span.

Herfried, sorry about that. I didn't realize that the original poster was wanting to display
a passage of time.   My intent was not to cause a problem.
I have been working on an old DOS based , database system conversion and the original
database engine stored the time of day in seconds.  I had posted a question here some time
back on how to convert those seconds back to a readable time format including AM/PM
and Chris had helped me with that. For some reason, I thought the original poster had
been looking for a similar solution.  I guess that is what happens when I have not had my
full 3 cups of coffee before reading and responding to posts!!!
james
Joergen Bech <jbech<NOSPAM>@ - 27 Dec 2005 17:32 GMT
>Herfried, sorry about that. I didn't realize that the original poster was wanting to display
>a passage of time.   My intent was not to cause a problem.
[quoted text clipped - 5 lines]
>full 3 cups of coffee before reading and responding to posts!!!
>james

's okay. The discussion took another direction because
the Date object and its ToString formatting options was suggested.

My replies (and yours) were to the specifics of those posts - not
to the problem of the original poster. I knew that we had gotten
off the track. No harm in that. As long as it is clear that those
replies are not specific to the problem outlined in the original post.

I sometimes throw something out there which might only be 80%
correct (e.g. something that works, turns out to be more easily done
another way). No harm in that *provided* someone else steps in
and offers the 100% solution.

When I had my Hex->Integer problem, I was happy to be informed
that Integer.Parse could handle it correctly. But even happier when
Armin suggested "System.Convert.ToInt32(hexvalue, 16)" which
was exactly what I needed.

Sometimes the Devil is in the details, but if the first replies to
any questions had to be 100% perfect, we might as well close
the group now. If, however, we help each other out, we'll eventually
get there.

/JB
Cor Ligthert [MVP] - 27 Dec 2005 15:53 GMT
James,

As it says AM/PM is only where the English language is used.

And simply enough use other languages 24hours clocks.

Although the way we say it by instance in Dutch is almost the same  as in
English.

20:00 is acht uur s'avonds (eight O'Clock in the evening)

:-)

Cor
james - 27 Dec 2005 16:37 GMT
> James,
>
[quoted text clipped - 9 lines]
>
> Cor

Cor, of course you are correct. I should have not answered the original post without
having a better understanding of what the original poster was trying to achieve. And
what his requirements were.
I'll try not to respond to posts unless I have had my full quota of coffee from now on. :-)
james

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.