Getting total hours in TimeSpan

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

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

+3
source share
2 answers

Then you want the TotalHoursproperty of the TimeSpanobject:

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

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

I did it as follows:

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
source

Source: https://habr.com/ru/post/1749601/


All Articles