Time span calculation for hours and minutes in C#

六眼飞鱼酱① 提交于 2019-12-05 21:58:04
TimeSpan span = toTime - fromTime;
// Split into hours:minutes: span.Hours, span.Minutes
// Total span in hours: span.TotalHours
// Total span in minutes (hours * 60): span.TotalMinutes

If you deduct a DateTime from a DateTime, the result is a a TimeSpan. So you can simply take

TimeSpan result = toTime-fromTime;
int hours = result.Hours;
int minutes = result.Minutes;
TimeSpan span = toTime - fromTime;
public double DurationinMins(DateTime startDt, DateTime endDt)
    {
        var ts = endDt.Subtract(startDt);
        return ts.TotalMinutes;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!