【发布时间】:2014-07-20 13:00:06
【问题描述】:
我正在尝试将秒转换为小时、分钟和秒。
例子:
int totalseconds = 5049;
如何使用一个消息框在表单中显示结果:
H:1 M:24 S:9
【问题讨论】:
-
你在哪里卡住了?除以 60?
标签: c#
我正在尝试将秒转换为小时、分钟和秒。
例子:
int totalseconds = 5049;
如何使用一个消息框在表单中显示结果:
H:1 M:24 S:9
【问题讨论】:
标签: c#
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);
【讨论】:
试试这个:
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));
希望对你有帮助
【讨论】:
您可以使用 TimeSpan:
var ts = TimeSpan.FromSeconds(totalsecond);
MessageBox.Show(string.Format("H: {0} M:{1} S:{2}", ts.Hours, ts.Minutes, ts.Seconds));
【讨论】:
TotalHours而不是Hours,以防秒数超过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);
【讨论】: