你可以使用Python中的datetime库来实现这个需求。下面是一个示例代码来获取上个月和本月的第一天和最后一天的时间戳:

from datetime import datetime, timedelta

# 获取当前时间
now = datetime.now()

# 如果日期小于15,获取上个月的时间戳
if now.day < 15:
    # 获取上个月的第一天零点时间戳
    last_month_first_day = datetime(now.year, now.month - 1, 1)
    last_month_first_day_ts = int(last_month_first_day.timestamp())

    # 获取上个月的最后一天零点时间戳
    last_month_last_day = datetime(now.year, now.month, 1) - timedelta(days=1)
    last_month_last_day_ts = int(last_month_last_day.replace(hour=0, minute=0, second=0).timestamp())

    print('上个月第一天零点时间戳:', last_month_first_day_ts)
    print('上个月最后一天零点时间戳:', last_month_last_day_ts)

# 如果日期大于等于15,获取本月的时间戳
else:
    # 获取本月的第一天零点时间戳
    this_month_first_day = datetime(now.year, now.month, 1)
    this_month_first_day_ts = int(this_month_first_day.timestamp())

    # 获取本月的最后一天零点时间戳
    this_month_last_day = datetime(now.year, now.month + 1, 1) - timedelta(days=1)
    this_month_last_day_ts = int(this_month_last_day.replace(hour=0, minute=0, second=0).timestamp())

    print('本月第一天零点时间戳:', this_month_first_day_ts)
    print('本月最后一天零点时间戳:', this_month_last_day_ts)

请注意,上述代码中的时间戳是以秒为单位的整数值。


原文地址: https://www.cveoy.top/t/topic/p9aQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录