scripts/ttt/month_ts.py

27 lines
1.1 KiB
Python
Raw Normal View History

2024-06-17 19:01:16 +08:00
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}")