python datetime 获取时间

# 日期操作, date
 
# 设置日期
>>> datetime.date(2020,3,13)
datetime.date(2020, 3, 13)
 
>>> datetime.date.today()
datetime.date(2020, 3, 12)
 
 
# 访问日期
>>> datetime.date.today()
datetime.date(2020, 3, 12)
>>> datetime.date.today().year
2020
>>> datetime.date.today().month
3
>>> datetime.date.today().day
12
 
 
>>> datetime.date.today().__getattribute__("year")
2020
>>> datetime.date.today().__getattribute__("month")
3
>>> datetime.date.today().__getattribute__("day")
12
 
# 返回星期几
# 周一到周日,1到7
>>> datetime.date.today().isoweekday()
4
 
# 周一到周日,0到6
>>> datetime.date.today().weekday()
3
 
 
 
 
# 日期的格式化
>>> datetime.date(2020,3,13).isoformat()
'2020-03-13'
 
>>> datetime.date(2020,3,13).__str__()
'2020-03-13'
 
 
>>> datetime.date(2020,3,13).ctime()
'Fri Mar 13 00:00:00 2020'
 
>>> datetime.date(2020,3,13).strftime("%Y-%d-%m")
'2020-13-03'
 
>>> datetime.date(2020,3,13).__format__("%Y-%d-%m")
'2020-13-03'
 
 
# 时间操作 time
 
# 同时操作时间和日期
>>> datetime.datetime.now().__str__()
'2020-03-12 10:30:21.929071'
 
>>> datetime.datetime.now().isoformat()
'2020-03-12T10:30:30.457183'
 
>>> datetime.datetime.now().ctime()
'Thu Mar 12 10:30:38 2020'
 
>>> datetime.datetime.now().strftime("%Y-%d-%m %H-%M-%S")
'2020-12-03 10-31-47'
 
>>> datetime.datetime.now().__format__("%Y-%d-%m %H-%M-%S")
'2020-12-03 10-32-10'
 
# 两个时间点的差值
(a + datetime.timedelta(hours = 8)).ctime()
 
 
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now()
 
>>> (a - b).days
-1
 
>>> (a - b).seconds
86393

posted on 2020-03-12 10:41  庐州月光  阅读(6731)  评论(0编辑  收藏  举报