datetime处理日期和时间
from datetime import datetime #获取当前时间 print('获取当前时间') print(datetime.now()) #获取当前标准时间 print('获取当前标准时间') print( datetime.utcnow())
#用指定日期时间创建datetime
now=datetime(2017,5,23,12,20) print(now)
from datetime import datetime print('将以下字符串转换成datetime类型:') print(datetime.strptime('2017/9/30','%Y/%m/%d')) print(datetime.strptime('2017年9月30日星期六','%Y年%m月%d日星期六')) print(datetime.strptime('2017年9月30日星期六8时42分24秒','%Y年%m月%d日星期六%I时%M分%S秒')) print(datetime.strptime('9/30/2017','%m/%d/%Y')) print(datetime.strptime('9/30/2017 8:42:50','%m/%d/%Y %H:%M:%S'))
from datetime import datetime print('将以下datetime类型转换成字符串:') print( datetime(2017,9,28,10,3,43)) a=datetime(2017,9,28,10,3,43) print(a.strftime('%Y年%m月%d日%A,%H时%M分%S秒')) b=datetime(2017,9,30,9,22,17) print(b.strftime('%A,%B %d, %Y')) print(b.strftime('%m/%d/%Y %I:%M:%S %p')) print(b.strftime('%B %d,%Y'))
from datetime import datetime now=datetime(2017,9,28) print(now.strftime('今天是%Y年%m月%d日')) print(now.strftime('今天是这周的第%w天')) print(now.strftime('今天是今年的第%j天')) print(now.strftime('今周是今年的第%W周')) print(now.strftime('今天是当月的第%d天'))