Search code examples
pythondatetimepython-dateutil

Python dateutil returns YYYY-MM-DD, but I need it reversed , help please?


I've got a piece of script in python that parses a line containing date and time:

d = dateutil.parser.parse("%s %s" % (m.group('date'),
            m.group('time')), dayfirst=True)

it returns the date in YYYY-MM-DD. How can I get it to say Weekday DD Month YYYY? Or if that is not possible DD-MM-YYYY?

Additionally, it also gives the time in HH:MM:SS, but I only need HH:MM, is there a way to lose the seconds?

Thank you!

I'm a complete novice to python so hope that anyone can help me, thank you very much!


Solution

  • Use datetime module's strftime to change the format according to the docs.

    d = '2019-01-09'
    d = datetime.datetime.strptime(d, '%Y-%m-%d')
    print(d)
    d = d.strftime('%A %d %B %Y %H:%M') # %I:%M if you want 12 hour format
    print(d) # Wednesday 09 January 2019 00:00