python common date treatment methods

Today: today = datetime.date.today ()

昨天:yesterday = today - datetime.timedelta(days=1)

上个月:last_month = today.month - 1 if today.month - 1 else 12

Current timestamp: time_stamp = time.time ()

 

 

Various types of conversion

时间戳转datetime:datetime.datetime.fromtimestamp(time_stamp)

datetime turn timestamp: int (time.mktime (today.timetuple ()))

datetime转字符串:today_str = today.strftime("%Y-%m-%d")

字符串转datetime:today = datetime.datetime.strptime(today_str,"%Y-%m-%d")

Complement difference: today + datetime.timedelta (hours = 8)

 

Guess you like

Origin www.cnblogs.com/gaojr/p/12082370.html