简体   繁体   English

将格式“Thu Dec 10 13:25:26 UTC 2020”的日期转换为日期时间 python

[英]Convert date of format “Thu Dec 10 13:25:26 UTC 2020” to datetime python

I have a date string with format "Thu Dec 10 13:25:26 UTC 2020".我有一个格式为“Thu Dec 10 13:25:26 UTC 2020”的日期字符串。 I want to convert this to UTC datetime in python.我想将其转换为 python 中的 UTC 日期时间。 How can I do that?我怎样才能做到这一点?

I had a string of format "2021-02-02T21:39:35-08:00" and I was able to convert this using the following code我有一个格式为“2021-02-02T21:39:35-08:00”的字符串,我可以使用以下代码对其进行转换

from datetime import timezone
from dateutil import parser
parser.isoparse("2021-02-02T21:39:35-08:00").astimezone(timezone.utc)
>>datetime.datetime(2021, 2, 3, 5, 39, 35, tzinfo=datetime.timezone.utc)

I am not sure how to do that for the format (Thu Dec 10 13:25:26 UTC 2020) above.我不确定如何为上述格式(2020 年 12 月 10 日星期四 13:25:26 UTC 2020)做到这一点。

You can use strptime from datetime.datetime with desire format, or can use parser from dateutil try:您可以使用带有期望格式的datetime.datetime中的strptime ,或者可以使用dateutil中的解析器尝试:

from datetime import datetime
from dateutil import parser

date_str = 'Thu Dec 10 13:25:26 UTC 2020'

datetime_object = datetime.strptime(date_str, '%a %b %d %H:%M:%S %Z %Y')

print(datetime_object)

print(parser.parse(date_str))

output: output:

2020-12-10 13:25:26
2020-12-10 13:25:26+00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中以这种格式 2020-01-13T09:25:19-0330 获取当前日期时间? - How to get current datetime in this format 2020-01-13T09:25:19-0330 in Python? Python,将9元组UTC日期转换为MySQL日期时间格式 - Python, Convert 9 tuple UTC date to MySQL datetime format 如何将字符串日期(2020 年 11 月 13 日)转换为熊猫中的日期时间? - How to convert string date (Nov 13, 2020) to datetime in pandas? 我将此格式作为对象 2020-11-18 10:36:29.772234 +0000 UTC,想转换为日期时间 - I have this format as object 2020-11-18 10:36:29.772234 +0000 UTC, want to convert to date time 怎么把“ Thu Jun 5 10:59:10 CDT 2014”转换成python datetime对象? - How to convert “Thu Jun 5 10:59:10 CDT 2014” into python datetime object? 如何在 Python 中将 UTC/扩展格式转换为日期时间 - How to convert UTC/extended format to datetime in Python 在Python,postgresql中将字符串转换为DateTime到UTC格式 - Convert string into DateTime to UTC format in Python, postgresql 如何在Python中将日期时间格式从'2017-10-25 15:24:38'转换为'2017年10月25日03:24 pm' - How to convert date time format from '2017-10-25 15:24:38' to '25 Oct 2017 03:24 pm' in Python Python日期格式转换为日期时间 - Python date format Convert to datetime 如何使用python将datetime.date转换为Date.UTC - How to convert the datetime.date to Date.UTC using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM