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

如何在Python中获取任意月份首尾的Unix时间戳

嘿,很高兴能帮到你!关于获取任意月份的起始和结束Unix时间戳,我有几个实用的方法可以分享,解决你遇到的字典无序和无法调用next()的问题:

方法一:用有序列表存储月份,遍历获取下一月

把你原来的字典改成列表,这样就能按顺序访问下一个月份的日期了,完美解决字典无序的问题:

import datetime

start_time = datetime.time(0, 0, 0, 0, tzinfo=datetime.timezone.utc)
end_time = datetime.time(23, 59, 59, 999, tzinfo=datetime.timezone.utc)

# 用列表存储有序的月份第一天
dates = [
    datetime.date(2019, 1, 1),
    datetime.date(2019, 2, 1),
    datetime.date(2019, 3, 1),
    datetime.date(2019, 4, 1)
]

for idx, current_month_first_day in enumerate(dates):
    # 生成当月起始时间
    start = datetime.datetime.combine(current_month_first_day, start_time)
    
    # 获取下一个月的第一天:如果是最后一个元素,就取下一年1月1日
    if idx == len(dates) - 1:
        next_month_first_day = datetime.date(current_month_first_day.year + 1, 1, 1)
    else:
        next_month_first_day = dates[idx + 1]
    
    # 计算当月最后一天,再结合结束时间
    last_day = next_month_first_day - datetime.timedelta(days=1)
    end = datetime.datetime.combine(last_day, end_time)
    
    do_stuff(start, end)

这个方法的好处是严格保持月份顺序,最后一个月份也能正确处理跨年情况。

方法二:动态计算下一月第一天(无需提前列所有月份)

如果不想提前把所有月份列出来,我们可以通过replace()方法动态生成下一月的第一天,不管给哪个月份都能直接计算:

import datetime

def get_month_start_end(year, month):
    start_time = datetime.time(0, 0, 0, 0, tzinfo=datetime.timezone.utc)
    end_time = datetime.time(23, 59, 59, 999, tzinfo=datetime.timezone.utc)
    
    # 当月第一天
    current_first_day = datetime.date(year, month, 1)
    start = datetime.datetime.combine(current_first_day, start_time)
    
    # 计算下一月第一天:如果是12月,就转到下一年1月
    if month == 12:
        next_first_day = datetime.date(year + 1, 1, 1)
    else:
        next_first_day = datetime.date(year, month + 1, 1)
    
    # 当月最后一天
    last_day = next_first_day - datetime.timedelta(days=1)
    end = datetime.datetime.combine(last_day, end_time)
    
    return start, end

# 调用示例:获取2019年1月的起止时间
jan_start, jan_end = get_month_start_end(2019, 1)
do_stuff(jan_start, jan_end)

# 遍历一整年的月份
for month in range(1, 13):
    start, end = get_month_start_end(2019, month)
    do_stuff(start, end)

这个方法最灵活,不管你要哪一年的哪个月份,直接传年份和月份就能得到结果,完全不用提前准备列表。

方法三:用第三方库python-dateutil简化操作

如果你能安装第三方库,dateutil的relativedelta可以让代码更简洁,不用手动处理12月转年的逻辑:
首先安装库:

pip install python-dateutil

然后使用:

import datetime
from dateutil.relativedelta import relativedelta

def get_month_start_end(year, month):
    start_time = datetime.time(0, 0, 0, 0, tzinfo=datetime.timezone.utc)
    end_time = datetime.time(23, 59, 59, 999, tzinfo=datetime.timezone.utc)
    
    current_first_day = datetime.date(year, month, 1)
    start = datetime.datetime.combine(current_first_day, start_time)
    
    # 直接加一个月得到下一月第一天,再减一天
    last_day = current_first_day + relativedelta(months=1) - datetime.timedelta(days=1)
    end = datetime.datetime.combine(last_day, end_time)
    
    return start, end

这个方法代码更简洁,逻辑更直观,适合经常处理日期的场景。

补充:获取最后一秒/最后一分钟

你原来的end_time设置已经能得到当月的最后一刻(23:59:59.999),如果只需要最后一分钟的话,把end_time改成datetime.time(23, 59, 0, 0, tzinfo=datetime.timezone.utc)就可以了,逻辑完全通用。

如果需要把datetime转换成Unix时间戳,只需要调用timestamp()方法:

unix_start = jan_start.timestamp()
unix_end = jan_end.timestamp()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:11:55