簡體   English   中英

python date time 獲取當前時間,但有秒和小時和分鍾

[英]python date time get the current time but with seconds and hour and minute

我能夠獲得當前時間,如下所示:

import datetime
str(datetime.date.today().strftime("%Y-%m-%d"))

那個打印

2016-06-26

但我需要添加分鍾、秒和小時

我在互聯網上搜索,人們建議使用這個:

str(datetime.date.today().strftime("%Y-%m-%d %H:%M"));

但實際上%H:%M總是返回00:00

你能幫忙嗎?

使用

>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2016-06-26 14:15:44'

你甚至可以使用.today()而不是.now()

datetime.date.today()<type 'datetime.date'> ,它基本上只返回日期。 這就是為什么您的小時和分鍾在打印時返回00:00

因此,如果您使用datetime.datetime.now()這將返回類型<type 'datetime.datetime'>這是一個datetime對象,您將獲得日期和時間。

datetime.date只處理日期,你需要使用datetime.datetime

您正在使用不包含任何時間的 datetime.date 對象。

你需要使用'datetime.datetime.now()'

試試這個:將返回一個帶有小時、分鍾和秒的字符串

datetime.datetime.now()..strftime("%Y-%m-%d %H:%M:%S")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM