This question has already been answered:
- Date conversion python 1 answer
How can the date of the form Mon, 26 Jun 2017 13:14:21 +0300 (MSK) (string) be brought to the form `2017-06-26 13:14:21 '?
This question has already been answered:
How can the date of the form Mon, 26 Jun 2017 13:14:21 +0300 (MSK) (string) be brought to the form `2017-06-26 13:14:21 '?
A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .
from dateutil.parser import parse d = parse('Mon, 26 Jun 2017 13:14:21 +0300 (MSK)') print d.strftime('%Y-%m-%d %H:%M:%S') # 2017-06-26 13:14:21 parse recognizes including the timezone:
print repr(d) # datetime.datetime(2017, 6, 26, 13, 14, 21, tzinfo=tzoffset(u'MSK', 10800)) A bit with a crutch that removes the last two "words" from the string, such as: +0300 (MSK) :
date_time_str = "Mon, 26 Jun 2017 13:14:21 +0300 (MSK)" date_time_str = ' '.join(date_time_str.split()[:-2]) from datetime import datetime dt = datetime.strptime(date_time_str, "%a, %d %b %Y %H:%M:%S") print(dt) # 2017-06-26 13:14:21 For python2, replace print(dt) with print dt
Read more about the date format here .
Since the input format is quite standard, you can use the parser from the dateutil package.
s = 'Mon, 26 Jun 2017 13:14:21 +0300 (MSK)' from dateutil import parser dt = parser.parse(s) print dt.replace(tzinfo=None) print , you must explicitly do str() . - insolorSource: https://ru.stackoverflow.com/questions/683850/
All Articles