• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

Convert seconds to hours:minute:second

Hi,

Would like to convert seconds to hours:minutes:seconds as such that 1 second = 00:00:01.

Thanks,

Yama
0
yamazed
Asked:
yamazed
  • 4
  • 4
  • 3
  • +2
1 Solution
 
ddunleaCommented:
Hi yamazed,

            System.DateTime oneSecond = new DateTime(0);
            oneSecond = oneSecond.AddSeconds(1);

If you then need to format it as 00:00:01, you can use:

            oneSecond.ToShortTimeString();

Regards
0
 
yamazedAuthor Commented:
Hi,

Try to convert 7200 seconds to HH:mm:ss using your lines of code and you will get 2:00 AM!! That is not right.

Yama
0
 
ddunleaCommented:
Well, it's not that far wrong either. I mean, 7200 seconds is 2 hours.

Anyway, try

oneSecond.ToLongTimeString();

And that should give you 02:00:00 as desired.

Regards
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
yamazedAuthor Commented:
Hi,

Well try 215115 seconds and you'll get 11:45:15 or 7777777777 will not give you correct hours:minutes:seconds because it only seems to parse a 24 hour clock.

What I desire is to get the actually hours:minutes:seconds from any given seconds integer.

Thanks,

Yama
0
 
gregoryyoungCommented:
hmm seem to me to be a much easier way ...

int hours = Math.Floor(TotalSeconds / 3600); // 60x60 give total hour parts
int minutes = Math.Floor((TotalSeconds - (hours * 3600)) / 60); //60 seconds
int seconds = TotalSeconds - hours * 3600 - minutes * 60
0
 
gregoryyoungCommented:
note I just typed it in quickly here, no compiler at this second you could just int math and allow the int to knock off the remainders instead of using round
0
 
gregoryyoungCommented:
           static void PrintSeconds(int TotalSeconds)
            {
                  TimeSpan t = new TimeSpan(0,0,TotalSeconds);
                  int hours = (int) t.TotalHours;
                  int minutes = (int) t.Minutes;
                  int seconds = (int) t.Seconds;
                  Console.WriteLine(hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString());
            }

works as well.

Cheers,

Greg
0
 
ddunleaCommented:
Hi Yamazed,

From the way you phrased your original question i.e. you listed the time as 00:00:01, I presumed (based on the use of 2 digits for the hours) that this would be an actual time of day, hence the use of the datetime structure as a quick and easy solution. If it's an arbitrary number of hours, then by all means, go with gregoryyoung's solution.

Regards
0
 
nachiketCommented:
Optimize the logic if u want... e.g. Use string builder etc
========================

public string GetStrTimeFromSces(int secs)
            {
                  
                  int hours = secs/3600;
                  int mins= (secs - hours*3600)/60;
                  secs = secs - hours*3600 - mins * 60;

                  string strSecs = secs.ToString();
                  string strMins = mins.ToString();
                  string strHours = hours.ToString();

                  if (secs < 10)  strSecs = "0" + strSecs;
                  if (mins < 10) strMins = "0" + strMins;
                  if (hours < 10) strHours = "0" + strHours;
                  
                  return  strHours.ToString() +":"+ strMins.ToString() +":"+ strSecs.ToString();
            }
0
 
yamazedAuthor Commented:
Hi Nachiket,

Here is a better way of doing this convertion:

 public string GetStringTimeFromSeconds(int seconds)
    {
      string newTime ;
      try
      {
        int hr = seconds/3600 ;
        int min = (seconds % 3600)/60 ;
        int sec = (seconds % 3600) % 60 ;
     
        string shr = hr.ToString() ;
        string smin = min.ToString() ;
        string ssec = sec.ToString() ;
 
        if (hr < 10)  
          shr = "0" + shr ;

        if (min < 10)
          smin = "0" + smin ;

        if (sec < 10)  
          ssec = "0" + ssec ;

        newTime = shr + ":" + smin  + ":" + ssec ;
      }
      catch(Exception)
      {
        newTime = "00:00:00";
      }  

      return newTime ;
    }

Yama
0
 
yamazedAuthor Commented:
Actually just one small change:

int hr = Math.Floor(seconds/3600) ;
int min = Math.Floor((seconds % 3600)/60) ;
int sec = Math.Floor((seconds % 3600) % 60) ;

Yama
0
 
gregoryyoungCommented:
I personally prefer using the timespan object but hey thats just me :)

also instead of going through all the ifs why not just use the ToString() method of the int ...

thus ending up with

         static void PrintSeconds(int TotalSeconds)
          {
               TimeSpan t = new TimeSpan(0,0,TotalSeconds);
               int hours = (int) t.TotalHours;
               int minutes =  t.Minutes;
               int seconds =  t.Seconds;
               Console.WriteLine(hours..ToString("0#") + ":" + minutes.ToString("0#") + ":" + seconds.ToString("#0"));
          }

0
 
Bob LearnedCommented:
I like the TimeSpan method, too :)

Bob
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now