2

I am trying to get the affected hours between 2 datetime and all i found was a python solution.

For example 'start' is 09:30 and 'end' is 14:00 (same day). The values I'd like returned are

[9:00, 10:00, 11:00, 12:00, 13:00, 14:00]

Python get whole hour values between 2 datetime objects

I can't seem to find any equivalent to C#.

4

9 回答 9

6

所以你想要两个日期之间所有时间的列表?您可以使用此查询:

TimeSpan ts =  dt2 - dt1;
IEnumerable<int> hoursBetween = Enumerable.Range(0, (int)ts.TotalHours)
    .Select(i => dt1.AddHours(i).Hour);

示例日期:

DateTime dt1 = new DateTime(2013, 07, 08, 15, 50, 00);
DateTime dt2 = new DateTime(2013, 07, 10, 19, 30, 00);
TimeSpan ts =  dt2 - dt1;

IEnumerable<int> hoursBetween = Enumerable.Range(0, (int)ts.TotalHours)
    .Select(i => dt1.AddHours(i).Hour);

foreach (int hour in hoursBetween)
    Console.WriteLine(hour);

输出:

15
16
17
18
19
20
21
22
23
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
于 2013-07-08T13:43:55.263 回答
3

以下将返回2 个 DateTime 对象之间的总小时数

(datevalue1 - datevalue2).TotalHours

对于自定义行为,例如显示小时列表,请在创建的时间跨度上使用简单的自定义方法,以获取所需格式的小时列表。

我头顶的代码建议:

public List<String> GenerateHours(DateTime t1,DateTime t2){
    if ((t2-t1).TotalHours >24)){
        //decide what to do.
        return null;
    }else{
        var currentHour = t2.Hour;
        var list = new List<String>();
        for (int i=0;i<(t2-t1).TotalHours;i++){
            if (currentHour<10){
                list.Add("0"+currentHour+":00");
            }else if (currentHour>=10){
                list.Add(currentHour+":00");
            }
            currentHour= (currentHour+1)%24;
        }
        return list;
    }
}
于 2013-07-08T13:43:35.867 回答
2
TimeSpan ts = DateTime1 - DateTime2;
double totalHours = ts.TotalHours;

来自MSDN:“获取以整小时和小数小时表示的当前 TimeSpan 结构的值。”

编辑:好的,现在我明白你的要求了。这个怎么样:

var d1 = DateTime.Today.AddHours(9.5);
var d2 = DateTime.Today.AddHours(14);

var first = new DateTime(d1.Year, d1.Month, d1.Day, d1.Minute == 0 ? d1.Hour : d1.Hour + 1, 0, 0);
var second = new DateTime(d2.Year, d2.Month, d2.Day, d2.Minute == 0 ? d2.Hour : d2.Hour + 1, 0, 0);

TimeSpan ts = second - first;

//returns DateTimes affected. I.e., Today at, [10:00, 11:00, 12:00, 13:00, 14:00]
IEnumerable<DateTime> dates = Enumerable.Range(0, (int)ts.TotalHours + 1).Select(hour => first.AddHours(hour));

//Or, if you just want the HOURs
//returns just ints: i.e., DateTimes 10,11,12,13,14
IEnumerable<int> hours = Enumerable.Range(0, (int)ts.TotalHours + 1).Select(hour => first.AddHours(hour).Hour);

如果您实际上有跨越几天的日期,则需要第一种方法。如果您不这样做,那么仅返回小时数的第二种方法可以正常工作。

于 2013-07-08T13:44:14.840 回答
2
public IEnumerable<DateTime> GetHoursBetween(DateTime start, DateTime end)
{
    DateTime first = start.Date.AddHours(start.Hour);
    for (DateTime dateTime = first; dateTime <= end; dateTime = dateTime.AddHours(1))
    {
        yield return dateTime;
    }
}
于 2013-07-08T13:58:17.370 回答
1

这应该可以解决问题。在 LinqPad 中测试。

var startDate = new DateTime(2013, 8, 7, 9, 30, 0);
var endDate = new DateTime(2013, 8, 7, 14, 0, 0);

List<string> times = new List<string>();

var currentTime = startDate;
if (currentTime.Minute != 0 || currentTime.Second != 0) {
    // Get next hour
    currentTime = currentTime.AddHours(1).AddMinutes(currentTime.Minute * -1);
}

while (currentTime <= endDate) {
    times.Add(string.Format("{0:00}:00", currentTime.Hour));
    currentTime = currentTime.AddHours(1);
}
于 2013-07-08T13:50:56.360 回答
0
(int)Math.Abs((date1 - date2).TotalHours)
于 2013-07-08T13:44:09.403 回答
0

只需将它们减去并从结果中获得总小时数。像这样的东西:

var totalHours = (dateTime1 - dateTime2).TotalHours;
于 2013-07-08T13:45:47.650 回答
0

也许这样的事情会起作用?

    public static List<DateTime> GetAffectedHours(DateTime start, DateTime end)
    {
        List<DateTime> result = new List<DateTime>();

        // Strip start of its minutes/seconds/etc
        DateTime initial = new DateTime(start.Year, start.Month, start.Day, start.Hour, 0, 0);

        // Go ahead and get the next hour
        DateTime iterator = initial.AddHours(1.0);

        // if it is still before the end
        while (iterator < end)
        {
            // add it to the results list
            result.Add(iterator);

            // and get the next hour
            iterator = iterator.AddHours(1.0);
        }

        return result;
    }
于 2013-07-08T13:53:23.133 回答
0

您可以使用For循环

List<int> allHoursBetween = new List<int>();

for (DateTime d = myStartDate; d <= myEndDate; d = d.AddHours(1))
     allHoursBetween.Add(d.Hour);
于 2013-07-08T13:54:13.950 回答