我有一个分钟值,想要得到两个字符串值,一个是小时数,另一个是分钟数,例如:
值 - 121分钟
字符串 hours = 2
字符串 minutes = 1
值 - 58分钟
字符串 hours = 0
字符串 minutes = 58
如何在C#中解决这个问题?
var span = System.TimeSpan.FromMinutes(121);
var hours = ((int)span.TotalHours).ToString();
var minutes = span.Minutes.ToString();
ToString()方法是因为你请求获取字符串值...int value = 121;
int hours = value / 60; // 2
int minutes = value % 60; // 1
string strHours = hours.ToString();
string strMinutes = minutes.ToString();
int value = 121;
int hours = value / 60;
int minutes = value % 60;
int value = 121;
TimeSpan timeSpan = TimeSpan.FromMinutes(value);
// gives you the rounded down value of 2
int hours = timeSpan.Hours;
// gives you the minutes left of the hour
int minutes = value - (hours * 60);