Python 使用 `time.strftime` 在格式化字符串中显示毫秒

标签 python python-3.x datetime string-formatting

我正在尝试将毫秒格式化为保留毫秒部分的格式化字符串。这是我的代码:

import time

time.strftime('%Y-%m-%d %H:%M:%S:%f', time.gmtime(1515694048121/1000.0))

1515694048121 是以毫秒为单位的时间。它返回我:

2018-01-11 18:07:28:f

正如我们所见,没有返回时间的毫秒部分。

包含毫秒时间部分的正确格式是什么? '%Y-%m-%d %H:%M:%S:%f' 不正确吗?

最佳答案

time.strftime(...) 中提到的 no 指令这将返回你毫秒。不确定您从哪里获得使用 %f 的引用。事实上time.gmtime(...)精度仅达到几秒。

作为 hack,为了实现这一点,您可以通过将毫秒值保留为来显式格式化字符串:

>>> import time

>>> time_in_ms = 1515694048121
>>> time.strftime('%Y-%m-%d %H:%M:%S:{}'.format(time_in_ms%1000), time.gmtime(time_in_ms/1000.0))
'2018-01-11 18:07:28:121'

这是有效指令的列表:

+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| Directive |                                                                                                   Meaning                                                                                                   | Notes |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| %a        | Locale’s abbreviated weekday name.                                                                                                                                                                          |       |
| %A        | Locale’s full weekday name.                                                                                                                                                                                 |       |
| %b        | Locale’s abbreviated month name.                                                                                                                                                                            |       |
| %B        | Locale’s full month name.                                                                                                                                                                                   |       |
| %c        | Locale’s appropriate date and time representation.                                                                                                                                                          |       |
| %d        | Day of the month as a decimal number [01,31].                                                                                                                                                               |       |
| %H        | Hour (24-hour clock) as a decimal number [00,23].                                                                                                                                                           |       |
| %I        | Hour (12-hour clock) as a decimal number [01,12].                                                                                                                                                           |       |
| %j        | Day of the year as a decimal number [001,366].                                                                                                                                                              |       |
| %m        | Month as a decimal number [01,12].                                                                                                                                                                          |       |
| %M        | Minute as a decimal number [00,59].                                                                                                                                                                         |       |
| %p        | Locale’s equivalent of either AM or PM.                                                                                                                                                                     | (1)   |
| %S        | Second as a decimal number [00,61].                                                                                                                                                                         | (2)   |
| %U        | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.                                | (3)   |
| %w        | Weekday as a decimal number [0(Sunday),6].                                                                                                                                                                  |       |
| %W        | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.                                | (3)   |
| %x        | Locale’s appropriate date representation.                                                                                                                                                                   |       |
| %X        | Locale’s appropriate time representation.                                                                                                                                                                   |       |
| %y        | Year without century as a decimal number [00,99].                                                                                                                                                           |       |
| %Y        | Year with century as a decimal number.                                                                                                                                                                      |       |
| %z        | Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. |       |
| %Z        | Time zone name (no characters if no time zone exists).                                                                                                                                                      |       |
| %%        |                                                                                                                                                                                                             |       |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+

关于Python 使用 `time.strftime` 在格式化字符串中显示毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48215948/

相关文章:

python 错误 "list indices must be integers",它们是整数

python - json.dump 时如何显式设置回车?

java - 时间戳格式匹配

python - 覆盖 Django Rest 中的 authToken View

python - 在 Python 的单元测试中比较 XML

python - Pandas 替换不会替换换行符 - 为什么?

python - Numpy:根据形状角选择任意形状

c# - 从DateTime的列表中找到最高的DateTime

c# - Ormlite 不会将日期时间从 SQL Server 映射到 C#?

python - 在无限循环中停止 python 脚本