简体   繁体   English

两次返回一天中的实际小时数

[英]Return actual hours of day between two times

I have a rather interesting problem at the minute where i want to return the actual hour values between two times. 我想在分钟之间返回两次之间的实际小时值,这是一个非常有趣的问题。 I dont mind if this is either in c# or mysql but i'm not sure what way to go about it. 我不介意这是在C#还是mysql中,但是我不确定该怎么做。

Eg Time 1= 13:00 & Time 2= 18:10 例如,时间1 = 13:00和时间2 = 18:10

i would like to return 13:00,14:00,15:00,16:00,17:00,18:00 我想返回13:00,14:00,15:00,16:00,17:00,18:00

There is plenty around for calculating the hour count between two times and i know i can use that integer value and increment my base hour as below but i was wondering if there was a cleaner way? 有很多地方可以计算两次之间的小时数,我知道我可以使用该整数值并按如下方式增加我的基本小时数,但是我想知道是否有更干净的方法?

Mysql Mysql的

timediff('2014-04-01 18:10:00', '2014-04-01 13:00:00' ) 

returns 05:10:00 返回05:10:00

hour('2014-04-01 13:00:00')

returns 13 返回13

increment the 13 by the hour value 将13乘以小时值

seems long winded :( 似乎long :(

I recommend working with DateTime and TimeSpan classes. 我建议使用DateTime和TimeSpan类。 You can use this: 您可以使用此:

double HoursBetween(DateTime t1, DateTime t2)
{
    return (t2 - t1).TotalHours;
}

You may want to apply some rounding if you need int return type. 如果需要int返回类型,则可能需要应用一些舍入。

What is happening is that the - operator for two DateTime objects is defined so a TimeSpan object is created which holds the time difference. 发生的情况是定义了两个DateTime对象的-运算符,因此创建了一个TimeSpan对象,该对象保留了时差。 TotalHours simply returns the TimeSpan value expressed in whole and fractional hours. TotalHours仅返回以整小时和小数小时表示的TimeSpan值。

In C# to enumerate all (full) hours from start to end : 在C#中枚举从startend所有(完整)小时:

public static IEnumerable<DateTime> Hours(DateTime start, DateTime end)
{
    start -= TimeSpan.FromMinutes(start.TimeOfDay.TotalMinutes);
    end -= TimeSpan.FromMinutes(end.TimeOfDay.TotalMinutes);

    return Enumerable.Range(0, (int)Math.Ceiling((end - start).TotalHours))
        .Select(x => begin + TimeSpan.FromHours(x));
}

First we remove minutes from interval boundaries, we need whole hours only (so 10:30 and 11:10 will result in two hours, not just the difference between them). 首先,我们从间隔边界中删除分钟,我们只需要整小时(因此10:30和11:10将导致两个小时,而不仅仅是两者之间的差额)。 Then we create an enumeration from zero to number of whole hours, we'll use it to create offsets from beginning. 然后,我们创建一个从零到整小时数的枚举,我们将使用它来创建从开始的偏移量。 I keep start DateTime using calculated TimeSpan as offset to keep track of date changes (for example if interval boundary is across two or more days). 我使用计算的TimeSpan作为偏移量来保持DateTime开始,以跟踪日期更改 (例如,如果间隔边界跨越两天或两天以上)。

If, for example, start is 2014/04/01 10:25 and end is 2014/04/01 13:20 then you'll get: 例如,如果start2014/04/01 10:25end2014/04/01 13:20那么您将获得:

2014/04/01 10:00
2014/04/01 11:00
2014/04/01 12:00
2014/04/01 13:00

To get just time part change return type to IEnumerable<TimeSpan> and last Select() to: 要仅获取时间零件更改,请返回类型为IEnumerable<TimeSpan> ,最后将Select()更改为:

.Select(x => (begin + TimeSpan.FromHours(x)).TimeOfDay);

In SQL it can be done but it's little bit more tricky because you need a working table with a sequence of integers. 在SQL中可以做到,但要复杂一点,因为您需要一个带有整数序列的工作表。 Please note following code is untested. 请注意,以下代码未经测试。

First you need to calculate that difference: 首先,您需要计算该差异:

CEIL(TIMESTAMPDIFF(MINUTE, startDate, endDate) / 60)

Now you can use a working table filled with numbers for offsets: 现在,您可以使用填充数字的工作表:

Value
-----
0
1
2
3
...

With this: 有了这个:

SELECT
    DATE_ADD(startDate, INTERVAL Value HOUR)
FROM 
    NumericTable
WHERE
    Value <= CEIL(TIMESTAMPDIFF(MINUTE, startDate, endDate) / 60)

In both cases (C# and SQL) you may replace output DateTime / Date with a TimeSpan / TIME object). 在这两种情况下(C#和SQL),您都可以将输出DateTime / Date替换为TimeSpan / TIME对象)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM