blu*_*sky 5 python datetime string-formatting python-3.x
我试图将毫秒格式化为格式化字符串,保留毫秒部分.这是我的代码:
import time
time.strftime('%Y-%m-%d %H:%M:%S:%f', time.gmtime(1515694048121/1000.0))
Run Code Online (Sandbox Code Playgroud)
其中1515694048121是以毫秒为单位的时间.它回报我:
2018-01-11 18:07:28:f
我们可以看到,毫秒时间内没有返回.
包含毫秒时间的正确格式是什么?是'%Y-%m-%d %H:%M:%S:%f'
不正确的?
没有提到的指令time.strftime(...)
将返回毫秒.不确定从哪里获得使用参考%f
.事实上time.gmtime(...)
,精度只有几秒钟.
作为一个黑客,为了实现这一点,你可以通过保留你的毫秒秒值显式格式化你的字符串:
>>> 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'
Run Code Online (Sandbox Code Playgroud)
这是有效指令列表:
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| 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). | |
| %% | | |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3179 次 |
最近记录: |