Search code examples
pythondate-formatstrftime

python change the date format using strftime


In python how do i change the format of the date i made Subtract between two dates:

lease_expiry_date = '{}'.format(t.lease_expiry_date.strftime("%d %B %Y"))
lease_commencement_date = '{}'.format(t.lease_commencement_date.strftime("%d %B %Y"))
years = t.lease_expiry_date - t.lease_commencement_date
p.add_run('{}'.format(years))

and the result was:

358 days, 0:00:00

i just want to show 358 days using strftime


Solution

  • Your object years is in Python a timedelta object, so you can specify which fields are of interest, e.g.:

    '{}'.format(years.days())