2017-05-05 109 views
1

我tryng以日期時間轉換成字符串。如何今天的日期時間轉換成時間戳

我正在尋找的這個question

答案時,我這樣做

time.mktime(time.strptime('2017-05-01 14:07:19', '%Y-%m-%d %H:%M:%S')) 

我能夠轉換日期時間爲時間標記,但我想演唱會當天的日期到這樣的時間戳:

timenow = datetime.datetime.now() 
//timenow = 2017-05-01 14:07:19 
time.mktime(time.strptime(timenow, '%Y-%m-%d %H:%M:%S')) 

那麼它會拋出錯誤TypeError: expected string or buffer即使我試圖像

time.mktime(time.strptime(str(timenow), '%Y-%m-%d %H:%M:%S')) 

則拋出ValueError: unconverted data remains: .067000

我怎麼能轉換日期時間今天爲timestamp

回答

3
import datetime 

now = datetime.datetime.now() 

print(now.strftime("%Y-%m-%d %H:%M:%S")) 
print(now.timestamp()) 
+0

thnkx alex7z .....我怎麼能刪除'***。0'從它的輸出我的意思刪除'.0' – Vivek

+0

當然。只需將其轉換爲int即可。 '打印(INT(now.timestamp()))' – alex7z

+0

ohh..its容易thnkx了很多我之前試過,但它拋出的錯誤,但現在它的格式轉換。 – Vivek