开发者

Getting the total hours in a TimeSpan

开发者 https://www.devze.com 2023-01-03 16:22 出处:网络
How can I subtract two dates and get the total hours of the TimeSpan 开发者_运维百科object that is returned?

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.


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;


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

精彩评论

暂无评论...
验证码 换一张
取 消