scripts/ttt/month_ts.py

27 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import datetime, timedelta
def get_first_day_of_current_and_next_month_timestamps(date=None):
if date is None:
date = datetime.now()
# 获取当前月份的第一天
first_day_of_current_month = datetime(date.year, date.month, 1)
# 获取下个月的第一天
# 首先计算下个月的第一天
next_month = date.month % 12 + 1 if date.month == 12 else date.month + 1
next_year = date.year if date.month != 12 else date.year + 1
first_day_of_next_month = datetime(next_year, next_month, 1)
# 将两个日期转换为时间戳
timestamp_current_month = first_day_of_current_month.timestamp()
timestamp_next_month = first_day_of_next_month.timestamp()
return int(timestamp_current_month), int(timestamp_next_month)
# 示例获取当前月和下一个月的第一天0点时间戳
current_month_timestamp, next_month_timestamp = get_first_day_of_current_and_next_month_timestamps()
print(f"Current month first day timestamp: {current_month_timestamp}")
print(f"Next month first day timestamp: {next_month_timestamp}")