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 '?

Reported as a duplicate by participants user207618, aleksandr barakin , Darth , Android Android , Pavel Mayorov Jun 29 '17 at 6:18 .

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 .

  • For what purpose? - andreymal

3 answers 3

 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) 
      • @Barton was faster) - insolor
      • But I do not write the output format) - mkkik
      • Clean, as the vehicle wrote that he wants to see at the exit. - mkkik
      • I understand everything. But in the end, if you need to save to a variable, and not just output via print , you must explicitly do str() . - insolor