【问题标题】:Python dateutil returns YYYY-MM-DD, but I need it reversed , help please? [duplicate]Python dateutil 返回 YYYY-MM-DD,但我需要它反转,请帮忙? [复制]
【发布时间】:2019-01-09 16:37:40
【问题描述】:

我在 python 中有一段脚本可以解析包含日期和时间的行:

d = dateutil.parser.parse("%s %s" % (m.group('date'),
            m.group('time')), dayfirst=True)

它返回 YYYY-MM-DD 中的日期。我怎样才能说工作日 DD 月 YYYY?或者如果这不可能DD-MM-YYYY?

另外,它还给出了 HH:MM:SS 中的时间,但我只需要 HH:MM,有没有办法丢失秒数?

谢谢!

我是python的新手,希望有人能帮助我,非常感谢!

【问题讨论】:

标签: python datetime python-dateutil


【解决方案1】:

使用 datetime 模块的 strftime 根据文档更改格式。

d = '2019-01-09'
d = datetime.datetime.strptime(d, '%Y-%m-%d')
print(d)
d = d.strftime('%A %d %B %Y %H:%M') # %I:%M if you want 12 hour format
print(d) # Wednesday 09 January 2019 00:00

【讨论】:

  • 非常感谢您,非常感谢您!
【解决方案2】:
from datetime import date, timedelta

day = date.today() # this gives you output in default YYYY-MM-DD format

now = datetime.now() # this give your output in default YYYY-MM-DD hh:mm:ss.millisec format

timestamp = now.strftime('%d-%m-%Y-%H:%M') # this gives you the output in the format you are looking for. See the screenshot

查看此链接了解更多详情:https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

【讨论】:

    猜你喜欢
    • 2016-12-16
    • 2016-10-17
    • 1970-01-01
    • 2014-05-30
    • 2019-01-03
    • 2014-05-28
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多