-2

how can I convert the date format from

Current format : [day] [month] [day value] [hour]:[minute]:[second] [time zone difference] [year]

to

New format : [year]-[month value]-[day value] [hour]:[minute]:[second]

For example, a current format value:

Tue Feb 04 17:04:01 +0000 2020 should be converted to:

2020-02-04 17:04:01

in python

1
  • 1
    datetime.strftime(datetime.strptime("%a %b %m %H:%M:%S %z %Y"), "%Y-%m-%d %H:%M:%S") should do it. See strftime.org for an explanation of the format codes. Commented May 26, 2022 at 10:29

1 Answer 1

0

You can use parser. Then use strftime to format the datetime object

from dateutil import parser

x = 'Tue Feb 04 17:04:01 +0000 2020 '
y = parser.parse(x).strftime('%Y-%m-%d %T')
>>> y
'2020-02-04 17:04:01'

Check out:

http://strftime.net/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.