python 获取时间戳

计算下一天 get next day

import time
import datetime
from datetime import datetime, timedelta

date_N_days_ago = datetime.now() + timedelta(days=1)
x = date_N_days_ago.strftime("%Y-%m-%d")
next_day  = int(time.mktime(time.strptime(x, "%Y-%m-%d")))
curr_time = int(time.time())
to_next_day_time = next_day - curr_time
print('curr_time', curr_time)
print('next_day',  next_day)
print('to_next_day_time',  to_next_day_time)

python 获取本月 本周开始的时间戳

import datetime
import time
    
def get_week_timestamp(self):
    weekday = datetime.datetime.now().weekday() # return [0, 6]
    week = datetime.datetime.now() - datetime.timedelta(days=weekday) # 本周开始的时候
    week = week.strftime("%Y-%m-%d")
    timestamp = int(time.mktime(time.strptime(week, "%Y-%m-%d")))
    return timestamp

def get_month_timestamp(self):
    month = datetime.datetime.now().strftime("%Y-%m")
    timestamp = int(time.mktime(time.strptime(month, "%Y-%m"))) # 本月开始的时候
    return timestamp

你可能感兴趣的:(python 获取时间戳)