seconds to time HH:MM

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

in my db i have the time as seconds, 3600, 1800, etc. how can i convert that
to the actual time and display it as 1:00 AM, etc?

thx
 
Barney said:
in my db i have the time as seconds, 3600, 1800, etc. how can i convert that
to the actual time and display it as 1:00 AM, etc?

thx


Here's the function i use to show time in HH:MM:SS by given time in seconds
It doesn't show AM/PM and doesn't detect next day (HH > 24), just because i didn't use that.
But it's easy to add

public string FormatTime(int ASecTime)
{
string res = "";
int hr = ASecTime / 3600;
int min = (ASecTime % 3600) / 60;
int sec = ASecTime % 60;

res += (hr >= 10)? hr.ToString() : "0" + hr.ToString();
res += ":";
res += (min >= 10)? min.ToString() : "0" + min.ToString();
res += ":";
res += (sec >= 10)? sec.ToString() : "0" + sec.ToString();
return res;
}


Hope it helps,
Andrey
 
If you mean that the time is stored as seconds from midnight, you could
use something like this:

using System;

class App
{
static void Main()
{
DateTime dt = DateTime.Today;
dt = dt.AddSeconds(1800);
Console.WriteLine(dt.ToShortTimeString());
}
}

Regards,
Joakim
 
Use the DateTime(int, int, int) constructor. Someone showed a way of doing
this using the fromseconds method, but I don't know that off the top of my
head.

Chris
 
Back
Top