PC Review


Reply
Thread Tools Rate Thread

How do I fill with 0 where nessessary when dealing with time.

 
 
tony
Guest
Posts: n/a
 
      15th Jun 2006
Hello!

I have this method that take number of seconds(numOfSec) and convert this
into a string with this format hh:mm:ss.
Now to my question there is a small problem when I have time component that
is < 10.
If I pass 7862 seconds to this method I get back "2:11:2" but I want to get
back "02:11:02".
I want this method to fill with 0 where nessesary.
Is it an easy way to do this or do I have to split each time component into
a string.

private string ConvStdTimeToTimeFormat(int numOfSec)
{
return Convert.ToString (sec/3600) + ":" + Convert.ToString ((sec %
3600) / 60 ) + ":" +
Convert.ToString ((sec % 3600) % 60);
}

//Tony


 
Reply With Quote
 
 
 
 
Barry Kelly
Guest
Posts: n/a
 
      15th Jun 2006
"tony" <(E-Mail Removed)> wrote:

> I have this method that take number of seconds(numOfSec) and convert this
> into a string with this format hh:mm:ss.
> Now to my question there is a small problem when I have time component that
> is < 10.
> If I pass 7862 seconds to this method I get back "2:11:2" but I want to get
> back "02:11:02".


Console.WriteLine(TimeSpan.FromSeconds(7862));

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
Claes Bergefall
Guest
Posts: n/a
 
      15th Jun 2006
private string ConvStdTimeToTimeFormat(int numOfSec)
{
DateTime dt = new DateTime((long) numOfSec * 10000000);
return dt.ToString("hh:mm:ss");
}

/claes

"tony" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello!
>
> I have this method that take number of seconds(numOfSec) and convert this
> into a string with this format hh:mm:ss.
> Now to my question there is a small problem when I have time component
> that
> is < 10.
> If I pass 7862 seconds to this method I get back "2:11:2" but I want to
> get
> back "02:11:02".
> I want this method to fill with 0 where nessesary.
> Is it an easy way to do this or do I have to split each time component
> into
> a string.
>
> private string ConvStdTimeToTimeFormat(int numOfSec)
> {
> return Convert.ToString (sec/3600) + ":" + Convert.ToString ((sec %
> 3600) / 60 ) + ":" +
> Convert.ToString ((sec % 3600) % 60);
> }
>
> //Tony
>
>



 
Reply With Quote
 
james.curran@gmail.com
Guest
Posts: n/a
 
      15th Jun 2006
In the more general case, to print a number with leading zeros, use the
print specifier "d", follwed by the number of digits desired:

private static string ConvStdTimeToTimeFormat(int numOfSec)
{
int hours = numOfSec/3600;
int minutes = ((numOfSec % 3600) / 60 );
int secs = ((numOfSec % 3600) % 60);

return String.Format("{0:d2}:{1:d2}:{2:d2}", hours, minutes, secs);
}


tony wrote:
> Hello!
>
> I have this method that take number of seconds(numOfSec) and convert this
> into a string with this format hh:mm:ss.
> Now to my question there is a small problem when I have time component that
> is < 10.
> If I pass 7862 seconds to this method I get back "2:11:2" but I want to get
> back "02:11:02".
> I want this method to fill with 0 where nessesary.
> Is it an easy way to do this or do I have to split each time component into
> a string.
>
> private string ConvStdTimeToTimeFormat(int numOfSec)
> {
> return Convert.ToString (sec/3600) + ":" + Convert.ToString ((sec %
> 3600) / 60 ) + ":" +
> Convert.ToString ((sec % 3600) % 60);
> }
>
> //Tony


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dealing with time Rhydderch Microsoft Excel Worksheet Functions 4 12th Jan 2009 10:56 PM
nessessary components? Kye Support 7 27th Oct 2004 08:51 AM
sp2, isit nessessary? kyle Windows XP General 4 25th Aug 2004 12:04 AM
Dealing with Daylight Savings and Time Zones lg Microsoft Outlook Calendar 1 2nd Jun 2004 02:31 AM
Forcing "Not IsPostBack" code to run on postback if nessessary??? Earl Teigrob Microsoft ASP .NET 2 23rd Dec 2003 06:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:06 AM.