1
嗨,我遇到麻煩時兩小時前在一個類似的功能我get_timestamp =>獲取GMT時間兩小時前的Python 2.7
def get_timestamp():
"""Return correctly formatted timestamp"""
return strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
def two_hours_ago():
"""Return correctly formatted timestamp"""
last = (date.today() - timedelta(hours=1))
return strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()-'2HOURS')
我嘗試這樣做:
def two_hours_ago():
"""Return correctly formatted timestamp"""
today = date.today()
yesterday = (today - timedelta(hours=2))
print(yesterday.timetuple())
return strftime("%Y-%m-%dT%H:%M:%SZ", yesterday.timetuple())
UPDATE 謝謝 黃燕浩
我正在尋找亞馬遜MWS的ISO格式返回2小時的間隔,我用下面的函數返回格式正確的時間。
def two_hours_ago():
"""Return correctly formatted timestamp"""
now = datetime.now()
two_hours_ago = (now - timedelta(hours=2))
return two_hours_ago.strftime("%Y-%m-%dT%H:%M:%SZ")
def now():
"""Return correctly formatted timestamp"""
now = datetime.now()
two_hours_ago = (now - timedelta(hours=0))
return two_hours_ago.strftime("%Y-%m-%dT%H:%M:%SZ")
一個'date'對象沒有時間。所以'今天 - timedelta(小時= 2)'是無意義的。 –
如果你想使用UTC日期和時間,你可以使用'datetime.datetime.utcnow()'。 –
_I試過這個_它做了什麼,它是如何不符合你的期望的? –