You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Python代码生成按2小时间隔的日期时间范围?

修改Python代码生成2小时间隔的日期时间范围

原代码及当前输出

原实现代码

from datetime import timedelta, date,datetime

def daterange(date1, date2):
    # Iterate over the range of days between date1 and date2
    for n in range(int((date2 - date1).days) + 1):
        # Yield each date within the range
        yield date1 + timedelta(n)

##Logic to fetch data in daterange
current_date = datetime.today()
# day = int(current_date.strftime('%d'))
day = 19
year = int(current_date.strftime('%Y'))
month = int(current_date.strftime('%m'))

if str(day) == '19':
    # Define the start date
    start_dt = date(year, month, 18)
    # Define the end date
    end_dt = date(year, month, day)
elif str(day) == '28' or str(day) == '30' or str(day) == '31' or str(day) == '29':
    # Define the start date
    start_dt = date(year, month, 16)
    # Define the end date
    end_dt = date(year, month, day)

# Iterate over the range of dates generated by the daterange function
for dt in daterange(start_dt, end_dt):
    # date in the format YYYY-MM-DD
    dated = dt.strftime("%Y-%m-%d")
    startdate = str(dated) + 'T00:00:00Z'
    enddate = str(dated) + 'T03:00:00Z'
    print(startdate)
    print(enddate)

当前输出

2024-06-18
2024-06-18T00:00:00Z
2024-06-18T03:00:00Z
2024-06-19
2024-06-19T00:00:00Z
2024-06-19T03:00:00Z

期望输出

2024-06-18T00:00:00Z
2024-06-18T02:00:00Z
2024-06-18T02:00:00Z
2024-06-18T04:00:00Z
2024-06-18T04:00:00Z
2024-06-18T06:00:00Z
2024-06-18T06:00:00Z
2024-06-18T08:00:00Z......
2024-06-18T22:00:00Z
2024-06-19T00:00:00Z......
2024-06-19T00:00:00Z
2024-06-19T02:00:00Z....

修改后的代码

from datetime import timedelta, datetime

def timerange(start_datetime, end_datetime, interval_hours=2):
    # 按指定小时间隔生成时间范围
    current = start_datetime
    while current <= end_datetime:
        yield current
        current += timedelta(hours=interval_hours)

# 获取当前日期
current_date = datetime.today()
day = 19  # 实际场景可替换为 int(current_date.strftime('%d'))
year = current_date.year
month = current_date.month

# 确定日期范围的起始和结束(转为包含时间的datetime对象)
if day == 19:
    start_dt = datetime(year, month, 18, 0, 0, 0)
    end_dt = datetime(year, month, day, 23, 59, 59)
elif day in (28, 29, 30, 31):
    start_dt = datetime(year, month, 16, 0, 0, 0)
    end_dt = datetime(year, month, day, 23, 59, 59)

# 生成完整的时间点序列
time_points = list(timerange(start_dt, end_dt))

# 遍历输出成对的2小时区间
for i in range(len(time_points) - 1):
    start = time_points[i].strftime("%Y-%m-%dT%H:%M:%SZ")
    end = time_points[i+1].strftime("%Y-%m-%dT%H:%M:%SZ")
    print(start)
    print(end)

关键修改说明

  • 替换原daterange函数为timerange:支持基于datetime对象生成指定小时间隔的时间点,同时覆盖日期和时间部分。
  • 日期处理改为datetime类型:原代码用date对象仅能处理日期,改为datetime后可包含时分秒,满足小时级间隔的需求。
  • 区间生成逻辑:通过遍历时间点序列,将相邻两个时间点作为一组区间的起始和结束,实现连续的2小时间隔输出。
  • 简化日期判断:将字符串判断改为整数判断,代码更简洁高效。

内容的提问来源于stack exchange,提问作者emiley mille

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:36:08