【问题标题】:How do I generate a python timestamp to a particular format?如何生成特定格式的 python 时间戳?
【发布时间】:2018-03-01 07:37:29
【问题描述】:

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

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

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

【问题讨论】:

标签: python timestamp


【解决方案1】:

您可以将日期时间与strftime 一起使用。示例:

import datetime

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

print(date)

将打印:

2017-09-20T12:59:43.888955

【讨论】:

【解决方案2】:

看下面的例子:

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

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

【讨论】:

  • 也可以使用now.isoformat(),我发现95%的时间都满足我的用例
猜你喜欢
  • 2014-11-02
  • 1970-01-01
  • 2014-10-04
  • 2021-10-27
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多