datetime 日期与时间
datetime库: 日期与时间
datetime主要包含的类有:
date 日期对象:常用的属性year,month,day
time 时间对象: hour,minute,second
datetime: date和time的合并
timedelta: 时间间隔
日期(date)
import datetime
date=datetime.date(2020,5,27)
print(date)
-->2020-05-27
时间(time)
time1=datetime.time(9,26,50)
print(time1)
--->09:26:50
日期时间(datetime)
datetime1=datetime.datetime(2020,5,27,9,26,50)
print(datetime1)
--->2020-05-27 09:26:50
返回当前系统时间(now)
now=datetime.datetime.now()
print(now)
--->2020-05-27 09:38:09.005255
将时间戳转换成易读的日期时间(fromtimestamp)
import time
t=time.time()
time2=datetime.datetime.fromtimestamp(t)
print(time2)
-->2020-05-27 09:38:09.005256
将日期时间转换成时间戳浮点数(timestamp)
t=datetime.datetime.now()
time3=datetime.datetime.timestamp(t)
print(time3)
-->1590543489.005255