-2

Is there a function in python that will return date in a string format : i.e 30/11/2015 will be returned as 30th November 2015 ? If the inputs was d=30,m=11,y=2015

Thanks for any help!

gweno10
  • 365
  • 1
  • 5
  • 16

1 Answers1

0

check datetime

generally you can do something like this:

import datetime

def special_datetime(d, m, y):
    s = datetime.datetime(day=d,month=m,year=y).strftime('%d.%B %Y')
    (d, my) = s.split('.')
    if int(d)%10 == 1:
        d += 'st '
    elif int(d) % 10 == 2:
        d += 'nd '
    elif int(d) % 10 == 3:
        d += 'rd '
    else:
        d += 'th '
    return d + my

The strftime behavior is described in the same documentation at the end

Rolf Lussi
  • 615
  • 5
  • 16
  • it doesn't print "20th November 2015" (note: the day is written as an ordinal number) – jfs Dec 01 '15 at 07:57
  • There is no format to directly return 1st, 2nd, ... So either you do it like in the answer for the [other question](http://stackoverflow.com/questions/5891555/display-the-date-like-may-5th-using-pythons-strftime) with a dictionary. I also edited the answer for you to provide a possible solution. – Rolf Lussi Dec 01 '15 at 08:49