-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

kulekhani
  • 5
  • 3
  • 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](https://strftime.org/) for an explanation of the format codes. – Henry May 26 '22 at 10:29

1 Answers1

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/

Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29