Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert Shell date format to Python date format

Can below piece of shell date format be converted to python date format?

date_used = $(date --date="$(date +%Y-%m-15) - 1 month" "+%Y_%m")

As per my understanding this above format is just taking day as 15 of current month and it simply subtracts 1 month and results in giving output in the format of year_month.

Output of the above shell date format –>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

echo $date_used = 2022_05

Can this particular scenario be done using python?

Any update about it would be really appreciable.

>Solution :

An equivalent would be:

from datetime import datetime,timedelta

# Current date, replace date with 15, subtract 30 days and format
date_used = (datetime.now().replace(day=15) - timedelta(days=30)).strftime('%Y_%m')
print(date_used)

Output:

2022_05
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading