2015-11-05 118 views
0

所以我用datetime模塊來打印日期並做到目前爲止。蟒蛇3.5.0 :::打印日期和時間

但是我不能打印時間...

我每次運行:

currentTime = datetime.time() 
print (currentTime) 

結果是:

00:00:00 

我想:

print(datetime.datetime.now()) 
>>>> 05-11-2015 19:00.173546 

但我只想要時間:19:00

你知道該怎麼做嗎?有沒有這個功能?

+1

打印 –

+0

@jeffcarey奏效。謝謝 –

+1

還檢出time.localtime()與time.strftime() –

回答

1

datetime.time()相當於datetime.time(hours=0, minute=0)

>>> import datetime 
>>> datetime.time() 
datetime.time(0, 0) 
>>> datetime.time(hour=0, minute=0) 
datetime.time(0, 0) 

datetime是一個模塊。 datetime.time是一類。 datetime.time()是使用默認小時,分鐘值(00)創建的該類的一個實例,例如,要創建與19:00相對應的time對象,您可以使用datetime.time(19, 0)

只打印時間爲現有datettime.datetimedatetime.time例如,你可以使用%H:%M time format:(datetime.datetime.now()時間())

>>> import datetime 
>>> current_time = datetime.datetime.now() 
>>> print("{:%H:%M}".format(current_time)) 
08:40 
0
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 
+0

酷:19:17:45.922373什麼意思是'.922373'? –

+0

很確定他們是微秒 – Busturdust

0

這樣的:

from datetime import datetime; 

t = datetime.now(); 
print '%d-%d' % (t.hour, t.minute); 
#prints 19:17 
+0

在我的打印19-31,但工程。謝謝 –

+0

是的,同一時區:) – DorinPopescu