我如何生成特定格式的 python 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46318714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:34:00  来源:igfitidea点击:

how do i generate a python timestamp to a particular format

pythontimestamp

提问by user3768071

in python how would i generate a timestamp to this specific format?

在 python 中,我将如何生成这种特定格式的时间戳?

2010-03-20T10:33:22-07

2010-03-20T10:33:22-07

I've searched high and low but couldn't the correct term that describes generating this specific format

我搜索了高低,但找不到描述生成这种特定格式的正确术语

回答by Henk Dekker

See the following example:

请参阅以下示例:

import datetime 
now = datetime.datetime.now()
now.strftime('%Y-%m-%dT%H:%M:%S') + ('-%02d' % (now.microsecond / 10000))

This could result in the following: '2017-09-20T11:52:32-98'

这可能导致以下结果:'2017-09-20T11:52:32-98'

回答by Lame Fanello

You can use datetime with strftime. Exemple:

您可以将 datetime 与strftime一起使用。例子:

import datetime

date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")

print(date)

Will print:

将打印:

2017-09-20T12:59:43.888955