0

This is a basic question but am getting tangled up

I have a string variable referencePeriodEndDate which contains a date with type string

Which I am trying to convert to a date only format

so '31/3/2017' to 2017-03-31

But am getting stuck. I've so far tried to use:

datetimeobject = datetime.strptime(referencePeriodEndDate,'%Y-%m-%d')
datetimeobject = referencePeriodEndDate.strftime('%Y-%m-%d')
halfer
  • 19,824
  • 17
  • 99
  • 186
Stacey
  • 4,825
  • 17
  • 58
  • 99

1 Answers1

2

if you can use the dateutil module

from dateutil import parser
dt =  parser.parse("31/3/2017")
print dt.strftime('%Y-%m-%d')

Output:

2017-03-31

Using datetime

import datetime
A = datetime.datetime.strptime('31/3/2017','%d/%m/%Y')
print A.strftime('%Y-%m-%d')
Rakesh
  • 81,458
  • 17
  • 76
  • 113