python 时间戳:查找 5 分钟前的所有时间戳作为字符串

python timestamp : Find all timestamps as string before 5 mins

我有一个像“201501010003”这样的字符串,表示像 YYYYMMDDHHMM.

这样的时间戳

我想返回 5 分钟并打印每分钟的所有时间戳,例如:

201501010002
201501010001
201501010000
201412122359
201412122358
from datetime import datetime, timedelta
s = "20150101000"
ts = datetime.strptime(s, '%Y%m%d%H%M')
for x in range(1,6):
    (ts - timedelta(minutes=x)).strftime('%Y%m%d%H%M')

输出:

'201412312359'
'201412312358'
'201412312357'
'201412312356'
'201412312355'

这是在循环中使用 timedelta 函数从您的字符串中一次减去一分钟。