Jump to content
Xtreme .Net Talk

Display Seconds (double) as HH:MM:SS (string?) [C#]


Recommended Posts

Posted

Given a Double value (dElapsedTime) which represents the amount of SECONDS elasped - what I need is to represent this value in the form hh:mm:ss (hour:minute:second).

Problem is I have noticed TimeSpan doesn't support such formatting (TimeSpan is d.hh:mm:ss, I don't want the "Days" - this is used to caluclate the amount oh hours a project was worked on for billing purposes).

Also - DateTime doesn't seem to be able to represent this correctly - I can get it in the hh:mm:Ss format (.ToString("HH:mm:ss");) BUT this represent a DateTime so if the HOURS go over 24hours it starts back at 0 instead of counting... (obviously this won't work)

 

So I need a way to convert SECONDS into HH:MM:SS - any clues/hints/help?

Worst-Case-Scenario I need to write a function that takes (double) and returns (string) and does the conversion, I just don't have a CLUE how to accomplish this task manually.

Thanks,

  • Administrators
Posted

Bit rough and ready (plus I've hardcoded the return format which is generally a bad idea) but the following should give you a starting point...

private string Test(int seconds)
{
TimeSpan ts = new TimeSpan(0,0,0, seconds, 0);
int Hours = (int) ts.TotalHours;

return string.Format("{0}:{1}:{2}", Hours,ts.Minutes, ts.Seconds);
}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...