1,datetime.now() # 获取当前datetime
datetime.utcnow()
>>> from datetime import datetime,timedelta >>> datetime.now()+timedelta(days=91)# 获取当前datetime datetime.datetime(2017, 12, 30, 10, 35, 1, 692406) >>> datetime.now() # 获取当前datetime datetime.datetime(2017, 9, 30, 10, 36, 19, 113406)
>>> datetime.strptime(‘2017年09月30日 8:42‘,‘%Y年%m月%d日 %H:%M‘)
datetime.datetime(2017, 9, 30, 8, 42) >>> datetime.strptime(‘9/30/2017‘,‘%m/%d/%Y‘) datetime.datetime(2017, 9, 30, 0, 0) >>> datetime.strptime(‘9/30/2017 08:42:24‘,‘%m/%d/%Y %X‘) datetime.datetime(2017, 9, 30, 8, 42, 24)
将以下datetime类型转换成字符串:
from datetime import datetime t=datetime(2017,9,28,10,3,43) print(t.strftime(‘%Y年%m月%d日星期4,%H时%M分%S秒‘)) s2=datetime(2017,9,30,9,22,17) print(s.strftime(‘%A,%B %d,%Y‘)) print(s.strftime(‘%m/%d/%Y %H:%M:%S AM‘)) print(s.strftime(‘%B %d,%Y‘))
from datetime import datetime time=datetime.now() print(‘今天是{0:%Y}年{0:%m}月{0:%d}日‘.format(time)) print(‘今天是这周的第%s天‘%time.strftime(‘%w‘)) print(‘今天是今年的第%s天‘%time.strftime(‘%j‘)) print(‘今天是今年的第%s周‘%time.strftime(‘%W‘)) print(‘今天是当月的第%s天‘%time.strftime(‘%d‘))
原文:http://www.cnblogs.com/YWEIEN/p/7614295.html