1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import datetime import time
today = datetime.date.today() first = today.replace(day=1) last_month = first - datetime.timedelta(days=1) period = last_month.strftime("%Y%m") print("当前日期(YYYYMM) => %s" % period)
now = datetime.datetime.now() date_str = now.strftime("%Y-%m-%d %H:%M:%S") print("当前时间(YYYY-MM-DD HH:mm:ss) => %s" % date_str)
result = datetime.datetime.strptime("2012-10-11 10:00:12", "%Y-%m-%d %H:%M:%S") print("字符串转时间 => %s" % result) print(str(result.year))
print("时间戳 => %s" % time.time())
d1 = datetime.datetime.strptime("2012-10-11", "%Y-%m-%d") d2 = datetime.datetime.strptime("2012-11-1", "%Y-%m-%d") delta = d2 - d1 print("相差 %s 天" % delta.days)
time.sleep(1)
|