Python中一些datetime的用法 from datetime import datetime, date, timedelta import time # 昨日日期 a = date.today() - timedelta(days=1) print(a) # 当日日期 b = datetime.now().strftime('%Y-%m-%d') print(b) # 当日星期 c = time.strftime("%w") print(c) # 上月月数 d = (date.today().replace(day=1) - timedelta(days=1)).strftime("%m") print(d) # 本月月数 e = datetime.now().month print(e) # 获取上星期日到上星期一的日期 now = datetime.now() last_week_start = (now - timedelta(days=now.weekday()+7)).strftime('%Y-%m-%d') last_week_end = (now - timedelta(days=now.weekday()+1)).strftime('%Y-%m-%d') print(last_week_start,last_week_end)
Python中datetime用法
最新推荐文章于 2024-09-17 09:28:23 发布