1、时间函数
- 获取当前时间戳:
ticks = time.time();
- 格式化当前日期
time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
- 日期转换时间戳
April = "2020-08-01"
timeArray = time.strptime(April,"%Y-%m-%d")
AprilStamp = time.mktime(timeArray)
- 时间戳转换日期
dateArr = datetime.datetime.fromtimestamp(AprilStamp)
AprilNew = dateArr.strftime("%Y-%m-%d %H:%M:%S")
- 本月第一天:
datetime.date(datetime.date.today().year,datetime.date.today().month,1)
- 上个月第一天
datetime.date(datetime.date.today().year,datetime.date.today().month-1,1)
如何根据某天计算上个月最后一天
1、获取其年、月 利用datetime.date函数
import time,datetime;
# 查看2020-8月的上个月最后天
April = "2020-08-01"
timeArray = time.strptime(April,"%Y-%m-%d")
#年
year1 = time.strftime("%Y", timeArray)
year1 = int(year1)
month1 = time.strftime("%m", timeArray)
month1 = int(month1)
print datetime.date(year1,month1,1)-datetime.timedelta(1)