-3

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!

Jan
  • 3
  • 1
  • 3
    You'll want to read up on the [datetime module](https://docs.python.org/3.7/library/datetime.html), particularly the [`strptime()` and `strftime()` functions](https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior). – glibdud Jan 09 '19 at 16:39

2 Answers2

0

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
Endyd
  • 1,249
  • 6
  • 12
0
from datetime import date, timedelta

day = date.today() # this gives you output in default YYYY-MM-DD format

now = datetime.now() # this give your output in default YYYY-MM-DD hh:mm:ss.millisec format

timestamp = now.strftime('%d-%m-%Y-%H:%M') # this gives you the output in the format you are looking for. See the screenshot

enter image description here

Have a look at this link for further details: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

anish
  • 88
  • 6