在 Python 3.3+ 中你可以使用timestamp() https://docs.python.org/3/library/datetime.html#datetime.datetime.timestamp:
>>> datetime.datetime(2012,4,1,0,0).timestamp()
1333234800.0
在 Python 3.2 或更早版本中,您可以显式执行此操作:
>>> (datetime.datetime(2012,4,1,0,0) - datetime.datetime(1970,1,1)).total_seconds()
1333238400.0
为什么你不应该使用datetime.strftime('%s')
Python 实际上并不支持 %s 作为 strftime 的参数(如果你检查http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior它不在列表中),它工作的唯一原因是 Python 将信息传递到系统的 strftime,它使用您的本地时区。
>>> datetime.datetime(2012,04,01,0,0).strftime('%s')
'1333234800'