首页 > 解决方案 > 将时间字符串“20180425.142117”转换为人类可读的格式?

问题描述

我希望这个字符串“20180425.142117”转换为人类可读的格式。例如 2018 年 4 月 25 日,下午 2:21 在 Python 中

任何想法如何做到这一点?

标签: pythondatedatetimetimedatetime-conversion

解决方案


尝试使用日期时间库。

import datetime as dt
time_str = "20180425.142117"
# Convert to a datetime 
time_dt = dt.datetime.strptime(time_str, '%Y%m%d.%H%M%S')
# Convert back to string with the right format
human_time = dt.datetime.strftime(time_dt, '%dth %b %Y, %I:%M%p')
print(human_time)

我不得不说,每次我需要做一些定制的事情时,我都会查找代码。


推荐阅读