python - ISO 格式的日期时间,毫秒到 000
问题描述
我的代码datetime.now(timezone.utc).astimezone(pytz.timezone("Europe/Berlin")).isoformat()
打印以下格式'2020-03-17T16:54:53.559415+01:00'
但我希望毫秒为 0000 '2020-03-17T16:54:53.000+01:00'
。
我已经尝试过使用replace(miliseconds=0)
,但它从打印的字符串中完全删除了毫秒。
解决方案
您想.replace(microsecond=0)
将毫秒(和微)秒归零。然后只需指定 ,timespec
的isoformat
权利'milliseconds'
:
nowzeroed = datetime.now(timezone.utc).replace(microsecond=0)
nowberlin = nowzeroed.astimezone(pytz.timezone("Europe/Berlin"))
nowformatted = nowberlin.isoformat(timespec='milliseconds')
推荐阅读
- python - 如何加快从 Python-Flask 应用程序返回 20MB Json 文件的速度?
- javascript - 谷歌表格 - 宏不会将图表的格式复制到另一个表格
- eclipse - Eclipse maven项目问题:缺少工件com.sun:tools:jar:1.8
- intersystems-cache - 将参数传递给全局、INTERSYSTEM CACHE 中的函数
- spring-boot - Spring-integration 中的@InboundChannelAdapter 没有连续运行?
- javascript - (Vanila JS DOM)为什么删除按钮不适用于所有“删除”类..?
- python - 如何显示此回归树?
- azure - 我可以将构建工件的 URL 传递给链接的 ARM 模板部署吗?
- python - 在方括号之间的初始子字符串之后提取文本
- python - 使用 export_inference_graph 时出错:TypeError: export_inference_graph() got an unexpected keyword argument 'use_side_inputs'