将开始时间和结束时间之间的差异转换为小时。分钟和秒

时间:2017-01-17 07:01:17

标签: c# datetime

我试图找到时间范围之间的持续时间并将其转换为小时,分钟和秒。应添加的小时数不应转换为24小时。如果有50小时30分30秒它应该显示为50:30:30,如果分钟持续时间超过59,它应该在几秒钟内增加到几小时。

请参阅完整的代码。这两种方法都没有给出正确的答案。方法GetSumOfDuration将0.30视为0.3,因此它没有给出正确的答案。方法GetSumOfDurationFromSecond在超过24小时的差异时失败。

using System;
using System.Collections.Generic;
using System.Linq;

namespace TimeCalculationApp
{
public class ProcessDetail
{
    public DateTime? StartTime { get; set; }
    public DateTime? EndTime { get; set; }

    public double? DurationInSeconds
    {
        get
        {
            if (StartTime != null && EndTime != null)
                return (EndTime.Value - StartTime.Value).TotalSeconds;
            return null;
        }
    }

    public double? Duration
    {
        get
        {
            if (StartTime != null && EndTime != null)
                return double.Parse(DateTimeHelper.GetTimeDuration(StartTime, EndTime));
            return null;
        }
    }
}

class DateTimeHelper
{
    public static string GetTimeDuration(DateTime? sDateTime, DateTime? eDateTime)
    {
        if (sDateTime != null && eDateTime != null)
        {
            if (sDateTime.Value <= eDateTime.Value)
            {
                var tsdiff = eDateTime.Value - sDateTime.Value;
                var ts = TimeSpan.FromSeconds(tsdiff.TotalSeconds);

                return string.Format("{0}.{1}", ts.Hours.ToString("0"), ts.Minutes.ToString("D2"));
            }
            return "0.00";
        }
        return "0.00";
    }

    public static string GetSumOfDuration(List<ProcessDetail> processDetails)
    {
        TimeSpan? sumDuration = null;

        var totalhours = 0;
        var totalminutes = 0;

        foreach (var processDetail in processDetails)
        {
            if (processDetail.Duration != null)
            {
                var splitString = processDetail.Duration.ToString().Split('.');

                var hour = splitString.Length > 0 ? splitString[0] : "0";
                var minutes = splitString.Length > 1 ? splitString[1] : "0";

                totalhours += int.Parse(hour);
                totalminutes += int.Parse(minutes);

                sumDuration = new TimeSpan(totalhours, totalminutes, 0);
            }
        }
        return sumDuration == null
            ? null
            : Math.Floor(sumDuration.Value.TotalHours).ToString("0") + ":" +
              sumDuration.Value.Minutes.ToString("D2");
    }

    public static string GetSumOfDurationFromSecond(List<ProcessDetail> processDetails, bool includeSeconds = false)
    {
        var seconds = processDetails.Where(x => x.DurationInSeconds != null).Sum(x => x.DurationInSeconds);
        if (seconds == null) return null;

        var ts = TimeSpan.FromSeconds(seconds.Value);

        return includeSeconds ? string.Format("{0}:{1}:{2}", ts.Hours.ToString("0"), ts.Minutes.ToString("D2"), ts.Seconds.ToString("D2")) :
            string.Format("{0}:{1}", ts.Hours.ToString("0"), ts.Minutes.ToString("D2"));
    }
}
class Program
{
    static void Main(string[] args)
    {
        var processDetails = GetProcessDetails();

        var duration = DateTimeHelper.GetSumOfDuration(processDetails);
        var durationFromSeconds = DateTimeHelper.GetSumOfDurationFromSecond(processDetails);

        Console.WriteLine(duration);
        Console.WriteLine(durationFromSeconds);

        processDetails = GetProcessDetailsTestData();

        duration = DateTimeHelper.GetSumOfDuration(processDetails);
        durationFromSeconds = DateTimeHelper.GetSumOfDurationFromSecond(processDetails);

        Console.WriteLine(duration);
        Console.WriteLine(durationFromSeconds);

        Console.ReadKey();

    }

    private static List<ProcessDetail> GetProcessDetails()
    {
        return new List<ProcessDetail>
        {
            new ProcessDetail
            {
                StartTime = new DateTime(2017, 01, 01, 1, 0, 0),
                EndTime =   new DateTime(2017, 01, 01, 7, 59, 0)
            },
            new ProcessDetail
            {
                StartTime = new DateTime(2017, 01, 01, 1, 0, 0),
                EndTime =   new DateTime(2017, 01, 01, 17, 3, 45)
            },
            new ProcessDetail
            {
                StartTime = new DateTime(2017, 01, 01, 1, 0, 0),
                EndTime =   new DateTime(2017, 01, 01, 10, 0, 20)
            },
            new ProcessDetail
            {
                StartTime = new DateTime(2017, 01, 01, 1, 0, 0),
                EndTime =   new DateTime(2017, 01, 01, 15, 1, 12)
            }
        };
    }

    private static List<ProcessDetail> GetProcessDetailsTestData()
    {
        return new List<ProcessDetail>
        {
            new ProcessDetail
            {
                StartTime =  DateTime.Parse("11/01/2017 06:36:28"),
                EndTime =  DateTime.Parse("11/01/2017 06:53:51")
            },
            new ProcessDetail
            {
                StartTime = DateTime.Parse("11/01/2017 09:12:46"),
                EndTime =  DateTime.Parse("11/01/2017 09:43:00")
            },
            new ProcessDetail
            {
                StartTime =DateTime.Parse("11/01/2017 15:29:25"),
                EndTime =   DateTime.Parse("11/01/2017 15:37:26")
            },
            new ProcessDetail
            {
                StartTime = DateTime.Parse("11/01/2017 15:19:19"),
                EndTime =  DateTime.Parse("11/01/2017 15:27:52")
            },
            new ProcessDetail
            {
                StartTime = DateTime.Parse("12/01/2017 01:05:43"),
                EndTime =  DateTime.Parse("12/01/2017 01:08:37")
            }
        };
    }
}
}

4 个答案:

答案 0 :(得分:2)

这应该给出日期之间的总小时数。

  DateTime dt1 = new DateTime(2016, 1, 1);
  DateTime dt2 = new DateTime(2016, 1, 5);
  TimeSpan difference = dt2 - dt1;
  int totalDaysToHours = difference.Days * 24; 
  Console.WriteLine((difference.Hours + totalDaysToHours) + " hours " + difference.Minutes + " Minutes " + difference.Seconds + " seconds" );
  Console.ReadKey();

答案 1 :(得分:0)

您可以通过减去操作来确定差异的时间跨度。

TimeSpan time1 = TimeSpan.Parse(date1);
TimeSpan time2 = TimeSpan.Parse(date2);

TimeSpan difference = time1 - time2;

int hours = difference.Hours;
int minutes = difference.Minutes;

答案 2 :(得分:0)

您可以从结束时间中减去开始时间。 结果将是Timespan对象。

// Set startime example to one hour ten minutes and 30 seconds ago
DateTime startTime = DateTime.Now.AddSeconds(-4230);
DateTime endTime = DateTime.Now;
TimeSpan elapsedTime = endTime-startTime;
// Print out the elapsed time in hour:minutes:seconds to string
string result = elapsedTime.TotalHours + ":" + elapsedTime.Minutes + ":" + elapsedTime.Seconds

请注意,TimeSpan为例如Hours和TotalHours提供了属性。小时数可以在0-23之间(当值超过23时,属性天数会增加)而TotalHours则提供完整小时数

答案 3 :(得分:0)

我同意ThomasVoß你应该使用Timespan。但是,TotalHours应该被转换为int以获得适当的小时数。

string result = (int)elapsedTime.TotalHours + ":" + elapsedTime.Minutes + ":" + elapsedTime.Seconds

如果您可以考虑以天数格式显示结果,则可以使用TimeSpan格式:

elapsedTime.ToString("dd\.hh\:mm\:ss") 

您可以阅读有关自定义TimeSpan格式here

的更多信息