如何根据天数计算小时数
问题描述:
我有总天数,我需要将其转换为几小时
i have total of days and i need to convert it to hours
DateTime date1 = new DateTime(DateTBeginWork.Value.Year, DateTBeginWork.Value.Month, DateTBeginWork.Value.Day, DTimeBeginWork.Value.Hour,DTimeBeginWork.Value.Minute, DTimeBeginWork.Value.Second);
DateTime date2 = new DateTime(DateTEndWork.Value.Year, DateTEndWork.Value.Month, DateTEndWork.Value.Day, DTimeEndWork.Value.Hour, DTimeEndWork.Value.Minute, DTimeEndWork.Value.Second);
TimeSpan ts = date2 - date1;
// TimeSpan ts = new TimeSpan();
ts = date2.Subtract(date1);
//the total of days is here
string NbhoursAday = ts.Days.ToString();
// DgNHours.DigitText = (NbhoursAday);
label2.Text =(ts.Hours.ToString());
label3.Text =(ts.Minutes.ToString());
答
查看TimeSpan.TotalHours和Timespan .TotalMinutes属性:
Look at the TimeSpan.TotalHours and Timespan.TotalMinutes properties:
DateTime dt1 = new DateTime(2013, 10, 1, 17, 45, 00);
DateTime dt2 = new DateTime(2013, 10, 12, 19, 07, 30);
TimeSpan dif = dt2 - dt1;
Console.WriteLine("{0}:{1}", dif.TotalHours,dif.Hours);
Console.WriteLine("{0}:{1}", dif.TotalMinutes, dif.Minutes);