python 3.5.0:::打印日期和时间
所以我使用datetime模块来打印日期,目前为止我已经做到了 但是我不能打印时间 每次我跑步时:python 3.5.0:::打印日期和时间,python,date,datetime,time,Python,Date,Datetime,Time,所以我使用datetime模块来打印日期,目前为止我已经做到了 但是我不能打印时间 每次我跑步时: currentTime = datetime.time() print (currentTime) 结果是: 00:00:00 我试过: print(datetime.datetime.now()) >>>> 05-11-2015 19:00.173546 但我只想要时间:19:00 你知道怎么做吗?有这样的功能吗?如下: date_time = datetime.
currentTime = datetime.time()
print (currentTime)
结果是:
00:00:00
我试过:
print(datetime.datetime.now())
>>>> 05-11-2015 19:00.173546
但我只想要时间:19:00
你知道怎么做吗?有这样的功能吗?如下:
date_time = datetime.datetime.now()
time = date_time.time()
# date_time is a datetime object tuple of year, month, day, hour, minnute, second, ms
# to access the time portion, simply call the time() function, which will return a tuple of only the hour, minue, second portion
# printing the time will give timestamp representation, but the object is actually of type datetime.time
from datetime import datetime;
t = datetime.now();
print '%d-%d' % (t.hour, t.minute);
#prints 19:17
datetime.time()
相当于datetime.time(小时=0,分钟=0)
:
是一个模块。这是一门课datetime.time()
是使用默认的小时、分钟值(0
,0
)创建的类的实例,例如,要创建与19:00
相对应的对象,可以使用datetime.time(19,0)
要仅打印现有或实例的时间,可以使用:
打印(datetime.datetime.now().time())@jeffcarey,效果不错。感谢您将time.localtime()与time.strftime()结合使用@jeffcarey AttributeError:type对象“datetime.time”没有属性“localtime”datetime.time与time.time不同-我相信他指的是时间moudleCool:19:17:45.922373什么意思“.922373”?非常确定它们是微秒是的,相同的时区:)
>>> import datetime
>>> datetime.time()
datetime.time(0, 0)
>>> datetime.time(hour=0, minute=0)
datetime.time(0, 0)
>>> import datetime
>>> current_time = datetime.datetime.now()
>>> print("{:%H:%M}".format(current_time))
08:40