Stacey
Stacey

Reputation: 5097

convert string date to date format

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')

Upvotes: 0

Views: 192

Answers (1)

Rakesh
Rakesh

Reputation: 82765

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')

Upvotes: 2

Related Questions