Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change a date to YYYYMMDD in Python? [duplicate]

I have the below code which works out the date 6 months ago from todays date.

import datetime
from dateutil.relativedelta import relativedelta
from datetime import date

six_months = date.today() + relativedelta(months=-6)  

However I would like six_months to be in "%Y%m%d" format.

Thank you for your help.

like image 259
Robert Miller Avatar asked Feb 05 '23 02:02

Robert Miller


1 Answers

You're looking for strftime.

six_months.strftime('%Y%m%d') 

This should do the job for you.

like image 150
Anomitra Avatar answered Feb 08 '23 15:02

Anomitra