Converting long dates in python. Wed Sep 07 10:35:00 IST 2022 trying to convert this into python. with help of strptime

Viewed 26
def date_convert(date_to_convert):
     return dt.datetime.strptime(date_to_convert, '%a %b %d %H:%M:%S %Z %Y').strftime('%m/%d/%Y')

data['Publish Time'].apply(date_convert)

when running this it says

"" ValueError: time data 'Wed Sep 07 10:35:00 IST 2022' does not match format '%a %b %d %H:%M:%S %Z %Y' ""

1 Answers

I'm unable to replicate the issue you're facing with the exact same code you've provided.

import datetime as dt

def date_convert(date_to_convert: str) -> str:
    return (dt.datetime.strptime(
                date_to_convert,'%a %b %d %H:%M:%S %Z %Y').
            strftime('%m/%d/%Y'))

date_convert('Wed Sep 07 10:35:00 IST 2022')  # 09/07/2022

However, you can also accomplish the task using the builtin dateutil library.

import dateutil

def date_convert(date_to_convert: str) -> str:
    return dateutil.parser.parse(date_to_convert).strftime('%m/%d/%Y')

date_convert('Wed Sep 07 10:35:00 IST 2022')  # 09/07/2022
Related