2016-04-29 61 views

回答

5

嗯,首先,你沒有得到正確now()。那是datetime.datetime,而不是頂層datetime。其次,它似乎並不像你試圖獲得你想要的格式字符串 - 它甚至沒有你指定的破折號。

>>> import datetime 
>>> now = datetime.now() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'module' object has no attribute 'now' 
>>> now = datetime.datetime.now() 
>>> now.strftime("%Y %m %d %H:%M") 
'2016 04 28 17:20' 
>>> now.strftime("%Y-%m-%dT%H:%M:%SZ") 
'2016-04-28T17:20:09Z' 
6

好吧,它看起來像你試圖讓你的日期對象的字符串表示形式爲ISO 8601格式。我不知道你是否真的需要Z末,這裏是另一種方式來.strftime()

>>> import datetime 
>>> now = datetime.datetime.now() 
>>> now.replace(microsecond=0).isoformat() 
'2016-04-28T17:29:53' 

我相信你可以計算出某事要追加Z字符串。

相關問題