【问题标题】:convert this format Tue Sep 07 2021 08:34:00 GMT+0530 (India Standard Time) to YYYY-MM-DD HH:MM:SS in python or django在 python 或 django 中将此格式转换为 2021 年 9 月 7 日星期二 08:34:00 GMT+0530(印度标准时间)为 YYYY-MM-DD HH:MM:SS
【发布时间】:2021-09-07 16:24:36
【问题描述】:

我正在尝试将此格式或任何时区格式转换为YYYY-MM-DD HH:MM:SS。 需要转换的格式是-Tue Sep 07 2021 08:34:00 GMT+0530 (India Standard Time)。

谁能帮忙?

【问题讨论】:

  • 您还应该粘贴您付出了哪些努力,编写了哪些代码以获得所需的结果。

标签: python python-3.x django django-rest-framework


【解决方案1】:
from datetime import datetime

date_string = 'Tue Sep 07 2021 08:34:00 GMT+0530 (India Standard Time)'.replace('(India Standard Time)', '').rstrip()
datetime_object = datetime.strptime(date_string, '%a %b %d %Y %H:%M:%S %Z%z').strftime("%Y-%m-%d %H:%M:%S")

print(datetime_object)

给出输出

2021-09-07 08:34:00

小时设置为 24 小时制

如果您想要 12 小时时钟,请将上述代码中的 %H 替换为 %I

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多