我想要一个datetime
字符串,从日期开始,以毫秒为单位。这段代码对我来说很典型,我很想学习如何缩短它。
from datetime import datetime
timeformatted= str(datetime.utcnow())
semiformatted= timeformatted.replace("-","")
almostformatted= semiformatted.replace(":","")
formatted=almostformatted.replace(".","")
withspacegoaway=formatted.replace(" ","")
formattedstripped=withspacegoaway.strip()
print formattedstripped
要获取毫秒(秒后3位小数)的日期字符串,请使用以下命令:
对于Python3.6,您可以使用:
输出:
这里有更多信息:https://docs.python.org/3/library/datetime.html#datetime.datetime.isoformat
http://docs.python.org/library/datetime.html#strftime-strptime-behavior
相关问题 更多 >
编程相关推荐