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

如何调整含timestamp与timedelta的代码,添加每日自定义结束时间戳

实现每日末尾添加自定义时间戳的方案

要给每日末尾加上23:59:24这个时间戳,你可以在原有生成15分钟间隔时间的逻辑里,加入判断每天结束时间的逻辑,同时避免重复添加和超出最终时间。以下是调整后的完整代码:

from datetime import datetime, timedelta

initial_date = datetime(1996, 11, 21, 0, 0, 0) 
final_date = datetime(2024, 4, 3, 23, 59, 24)   

time_interval = timedelta(minutes=15)

datetimes_list = []
current_date = initial_date

while current_date <= final_date:
    datetimes_list.append(current_date)
    
    # 生成当天的23:59:24时间戳
    current_day_end = datetime(current_date.year, current_date.month, current_date.day, 23, 59, 24)
    # 检查这个时间戳是否未被添加,且不超过最终结束时间
    if current_day_end > current_date and current_day_end <= final_date:
        if datetimes_list[-1] != current_day_end:
            datetimes_list.append(current_day_end)
    
    current_date += time_interval

# 去重并排序,确保时间序列有序且无重复
datetimes_list = sorted(list(set(datetimes_list)))

关键逻辑说明

  • 每次循环时,生成当天的23:59:24时间戳current_day_end
  • 通过判断确保这个时间戳既在当前日期的范围内(没被15分钟间隔的时间覆盖),又不超过设定的最终结束时间
  • 最后做一次去重和排序,防止出现重复的时间戳,保证列表的准确性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 09:55:01