标签:logs date class day 系统 datetime span 星期六 cno
1.datetime.now() # 获取当前datetime
datetime.utcnow()
>>> datetime.now() datetime.datetime(2017, 9, 30, 11, 20, 40, 535026) >>> datetime.utcnow() datetime.datetime(2017, 9, 30, 3, 20, 51, 546427) >>>
2.datetime(2017, 5, 23, 12, 20) # 用指定日期时间创建datetime
>>> datetime.strptime(‘2017,5,23,12,20‘, ‘%Y,%m,%d,%H,%M‘) datetime.datetime(2017, 5, 23, 12, 20)
3.将以下字符串转换成datetime类型:
from datetime import datetime dt1=datetime.strptime(‘2017/9/30‘,‘%Y/%m/%d‘) print(dt1) dt2=datetime.strptime(‘2017年9月30日星期六‘,‘%Y年%m月%d日星期六‘) print(dt2) dt3=datetime.strptime(‘2017年9月30日星期六8时42分24秒‘,‘%Y年%m月%d日星期六%H时%M分%S秒‘) print(dt3) dt4=datetime.strptime(‘9/30/2017‘,‘%m/%d/%Y‘) print(dt4) dt5=datetime.strptime(‘9/30/2017 8:42:50‘,‘%m/%d/%Y %H:%M:%S‘) print(dt5)
4.将以下datetime类型转换成字符串:
from datetime import datetime dt=datetime(2017,9,30,10,3,43) print(dt.strftime(‘%Y年%m月%d日%A,%H时%M分%S秒‘)) print(dt.strftime(‘%A,%B %d,%Y‘)) print(dt.strftime(‘%m/%d/%Y %I:%M:%S%p‘)) print(dt.strftime(‘%B %d,%Y‘))
5.用系统时间输出以下字符串:
from datetime import datetime now=datetime.now() a=now.strftime(‘%Y年%m月%d日‘) print(‘今天是‘,a) b=now.strftime(‘%w‘) print(‘今天是一周的第‘,b,‘天‘) c=now.strftime(‘%j‘) print(‘今天是今年的第‘,c,‘天‘) d=now.strftime(‘%W‘) print(‘今天是今年的第‘,d,‘周‘) e=now.strftime(‘%d‘) print(‘今天是当月的第‘,e,‘天‘)
标签:logs date class day 系统 datetime span 星期六 cno
原文地址:http://www.cnblogs.com/linf/p/7617871.html