- 积分
- 3638
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2014-10-21
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2020-6-4 09:30:01
|
显示全部楼层
本帖最后由 15195775117 于 2020-10-28 17:17 编辑
import datetime
print(datetime.date(2020,9,6))
#输出:2020-09-06
print(datetime.time(12,3,45))
#输出:12:03:45
print(datetime.datetime(2020,9,6,12,3,45))
#输出:2020-09-06 12:03:45
#日期时间比较:
print(datetime.date(2020,9,6) > datetime.date(2020,9,7))
#输出:False
print(datetime.time(12,3,45) > datetime.time(12,3,42))
#输出:True
print(datetime.datetime(2020,9,6,12,3,45) >
datetime.datetime(2020,8,6,12,3,45))
#输出:True
today=datetime.date.today()#今天
now=datetime.datetime.now()#现在
print('现在时间:',now)
#输出:现在时间: 2020-06-04 09:26:13.790593
print('现在世界时:',datetime.datetime.utcnow())
#输出:现在世界时: 2020-06-04 01:26:13.790593
print('今天:',today)
#输出:今天: 2020-06-04
print('数据类型:',type(today))
#输出:数据类型: <class 'datetime.date'>
#分离年月日:
print(today.year,today.month,today.day)
#输出:2020 6 4
print(now.year,now.month,now.day,now.hour,now.minute,now.second)
#输出:2020 6 4 9 26 13
format='%a %b %d %H:%M:%S %Y'
today=datetime.datetime.today()
print(today)
#输出:2020-06-04 09:26:13.790593
str_time=today.strftime(format)#datetime转字符串
print(type(str_time))
#输出:<class 'str'>
#字符串转字典/datetime:
dict_time=datetime.datetime.strptime(str_time,format)
print(type(dict_time))
#输出:<class 'datetime.datetime'>
print(dict_time.day)
#输出:4
|
|