Getting the total hours in a TimeSpan

两盒软妹~` 提交于 2019-12-23 16:47:09

问题


How can I subtract two dates and get the total hours of the TimeSpan object that is returned?

For example, if the TimeSpan is 2 days, the total hours are 48.


回答1:


Then you want the TotalHours property of the TimeSpan object:

DateTime today = DateTime.Today;
DateTime twoDaysAgo = today.AddDays(-2.0);

// returns 48.0
double totalHours = (today - twoDaysAgo).TotalHours;



回答2:


I did it this way:

int day = Tempo.Days;
int Hours = Tempo.Hours;
if (day > 0)
{
  for (int i = 0; i < day; i++)
  {
    Hours += 24;
  }
}
string minuto = (Tempo.Minutes < 9) ? "0" + Tempo.Minutes.ToString() : Tempo.Minutes.ToString();
string segundos = (Tempo.Seconds < 9) ? "0" + Tempo.Seconds.ToString() : Tempo.Seconds.ToString();
string texto = Hours.ToString() + ":" + minuto + ":" + segundos;


来源:https://stackoverflow.com/questions/3023514/getting-the-total-hours-in-a-timespan

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!