2

I'm trying to convert seconds to hours , minutes and seconds.

Example:

int totalseconds = 5049;

How do i use one message box to display the results in the form:

H:1 M:24 S:9
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
King Wizard
  • 39
  • 1
  • 2
  • 4

4 Answers4

8
  var timeSpan = TimeSpan.FromSeconds(5049);
    int hr = timeSpan.Hours;
    int mn = timeSpan.Minutes;
    int sec = timeSpan.Seconds;
    MessageBox.Show("H:" + hr + " M:" + mn + " S:" + sec);
Suji
  • 1,326
  • 1
  • 12
  • 27
7

Try this:

MessageBox message = new MessageBox();
int totalseconds = 5049;
int hours = totalSeconds / 3600;
int minutes = (totalSeconds % 3600) / 60;
int seconds = (totalSeconds % 3600) % 60;
message.ShowDialog(string.Format("{0}:{1}:{2}", hours, minutes, seconds));

I hope this helps

Oscar Bralo
  • 1,912
  • 13
  • 12
3

You can use a TimeSpan:

var ts = TimeSpan.FromSeconds(totalsecond);

MessageBox.Show(string.Format("H: {0} M:{1} S:{2}", ts.Hours, ts.Minutes, ts.Seconds));
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

Use TimeSpan to convert from seconds,

    var timeSpan = TimeSpan.FromSeconds(5049);
    int hh = timeSpan.Hours;
    int mm = timeSpan.Minutes;
    int ss = timeSpan.Seconds;
    MessageBox.Show("Hours" + hh + " Minutes" + mm + " Seconds" + ss);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396