给定2 datetimes的python获取24:00-06:00之间的小时数

时间:2018-08-27 07:24:14

标签: python-3.x

我正在尝试计算给定的日期时间范围,以获取24:00-06:00的夜间时间。

示例:

如果给定日期为8月27日23:00-8月29日01:00(25小时)。所以答案应该是晚上7个小时。

OR

如果给定的日期是8月27日05:00-8月29日07:00(50小时),那么答案应该是晚上13小时。

我知道我必须计算天数差异

diff_days = abs(convert_end_date - convert_start_date).days

,然后确定这些时间是否在2个给定的日期时间范围内。

我将其放置在while循环中,因为这取决于多少天。

希望这很清楚,因为我不知道还有什么比这更简单的解释了...

这是我所拥有的代码,但如果超过24小时则不包括...

convert_start_date = datetime.fromtimestamp(start_date)
convert_end_date = datetime.fromtimestamp(end_date)

min_midnight = datetime.combine(convert_start_date, time.min)
next_day = min_midnight + dt.timedelta(days=1)
six_oclock_next_day = next_day + dt.timedelta(hours=6)


# if between 00:00 - 06:00
    if min_midnight < convert_start_date < six_oclock_today and min_midnight < convert_end_date < six_oclock_today:
        night_hours = (convert_end_date - convert_start_date).seconds / 60
        print(night_hours / 60)
        print("If start date and end date between midnight and 6am")

    # if start date between 00:00 - 06:00 and end date not
    if min_midnight < convert_start_date < six_oclock_today and (
    not min_midnight < convert_end_date < six_oclock_today):
        night_hours = (six_oclock_today - convert_start_date).seconds / 60
        print(night_hours / 60)
        print("If start date between midnight and 6 am and not the end date")

    # if between 06:00 - 2400
    if six_oclock_today < convert_start_date < next_day and six_oclock_today < convert_end_date < next_day:
        day_hours = (convert_end_date - convert_start_date).seconds / 60
        print(day_hours / 60)
        print("If start date between 6am and midnight")

    # if start date between 06:00 - 24:00 and end date not
    if six_oclock_today < convert_start_date < next_day and (not six_oclock_today < convert_end_date < next_day):
        day_hours = (next_day - convert_start_date).seconds / 60
        print(day_hours / 60)
        print("If end date between 6am and midnight")

    # if start date between next day 00:00 - 06:00
    if next_day < convert_start_date < six_oclock_next_day:
        night_hours = (six_oclock_next_day - convert_start_date).seconds / 60
        print(night_hours / 60)
        print("If start date lower then next day 6 oclock")

    # if end date between next day 00:00 - 06:00
    if next_day < convert_end_date < six_oclock_next_day:
        night_hours = (convert_end_date - next_day).seconds / 60
        print(night_hours / 60)
        print("If end date between next day midnight and 6 oclock")

    # if end date next day after tomorrow 06:00-24:00
    if six_oclock_next_day < convert_end_date:
        night_hours = 6.0 * 60
        day_hours = day_hours + (convert_end_date - six_oclock_next_day).seconds / 60
        print(day_hours / 60 + night_hours)
        print("If end date the day after after 6 am")

谢谢你,祝你愉快。

1 个答案:

答案 0 :(得分:1)

此代码可以做到,但有一些限制:

def count_hours_between_00_and_06(start_date, end_date):
    if end_date < start_date:
        start_date, end_date = end_date, start_date

    start_hour = start_date.hour
    end_hour = end_date.hour
    if start_date.date() == end_date.date():
        #dates are in the same day

        if start_hour > 6:
            return 0
        else:
            end_hour = end_hour if end_hour < 6 else 6
            return end_hour - start_hour
    else:
        days_delta = (end_date.date() - start_date.date).days - 1
        total_hours = 0
        if start_hour < 6:
            total_hours += 6 - start_hour
        total_hours += end_hour if end_hour < 6 else 6
        total_hours += days_delta * 6
        return total_hours

请注意,它不会考虑datetime对象的分钟和秒部分,也不会考虑时区和夏时制,但会满足您的要求

我还应该注意,由于您的范围是从早上0点至早上6点,如果您要将要计算的小时数更改为凌晨1点至早上7点,则需要简化数学运算,以简化计算。