【问题标题】:Convert a time string "20180425.142117" to human readable format?将时间字符串“20180425.142117”转换为人类可读的格式?
【发布时间】:2018-10-06 12:22:42
【问题描述】:

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

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

【问题讨论】:

  • 那是什么表示?你试过谷歌搜索吗?
  • datetime.datetime.strptime("20180425.142117", "%Y%m%d.%H%M%S").ctime() -- 见docs.python.org/3.6/library/…

标签: python date datetime time datetime-conversion


【解决方案1】:

尝试使用datetime 库。

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)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 2021-06-28
    • 2016-11-29
    • 2020-04-12
    • 2014-01-30
    • 2011-04-07
    相关资源
    最近更新 更多