如何将总秒数值转换为字符串,以及“小时数秒”'格式

时间:2014-07-20 13:00:06

标签: c#

我试图将秒数转换为小时,分钟和秒。

示例:

int totalseconds = 5049;

如何使用一个消息框以表格形式显示结果:

H:1 M:24 S:9

4 个答案:

答案 0 :(得分:7)

  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);

答案 1 :(得分:5)

试试这个:

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));

我希望这会有所帮助

答案 2 :(得分:3)

您可以使用TimeSpan:

var ts = TimeSpan.FromSeconds(totalsecond);

MessageBox.Show(string.Format("H: {0} M:{1} S:{2}", ts.Hours, ts.Minutes, ts.Seconds));

答案 3 :(得分:1)

使用TimeSpan转换秒数,

    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);
相关问题