from datetime import datetime now = datetime.now() print(now) utcnow = datetime.utcnow() print(utcnow)
from datetime import datetime t=datetime(2017, 5, 23, 12, 20) print(t)
from datetime import datetime f1=datetime.strptime(‘2017/9/30‘,‘%Y/%m/%d‘) print(f1) f2=datetime.strptime(‘2017年9月30日星期六‘,‘%Y年%m月%d日星期六‘) print(f2) f3=datetime.strptime(‘2017年9月30日星期六8时42分24秒‘,‘%Y年%m月%d日星期六%H时%M分%S秒‘) print(f3) f4=datetime.strptime(‘9/30/2017‘,‘%m/%d/%Y‘) print(f4) f5=datetime.strptime(‘9/30/2017 8:42:50 ‘,‘%m/%d/%Y %H:%M:%S ‘) print(f5)
将以下datetime类型转换成字符串:
from datetime import datetime f1=datetime(2017,9,28,10,3,43) print(f1.strftime(‘%Y年%m月%d日%A%H时%M分%S秒‘)) f2=datetime(2017,9,30) print(f2.strftime(‘%A,%B %d,%Y‘)) f3=datetime(2017,9,30,9,22,17) print(f3.strftime(‘%m/%d/%Y %I:%M:%S %p‘)) print(f2.strftime(‘%B %d,%Y‘))
用datetime类型的变量输出以下字符串:
from datetime import datetime now =datetime.now() print(now.strftime(‘今天是%Y年%m月%d日‘)) print(now.strftime(‘今天是这周的第%w天‘)) print(now.strftime(‘今天是今年的第%j天‘)) print(now.strftime(‘今周是今年的第%W周‘)) print(now.strftime(‘今天是当月的第%d天‘))
原文:http://www.cnblogs.com/GAODASHANG/p/7622179.html