简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM