Working with ISO 8601 dates in Python Part II

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

If you happen to have stumbled upon this post I apologize, I’m not going to be doing a lot of explaining here. Rather, I really just wanted to post this little snippet of code for my own reference so I don’t lose it! But hey, maybe you’ll find it useful too…

from datetime import date, datetime
now = datetime.now()
print(now)
now_iso_8601 = now.isoformat()
print(now_iso_8601)
import time as time_
current_time = time_.time()
print(current_time)
# date.isoformat() vs date.fromisoformat
date_from_iso_format = date.fromisoformat('1987-02-06')
print(date_from_iso_format)
date_isoformat = date.isoformat(now)
print(date_isoformat)
# REFERENCE: https://topherpedersen.blog/2019/07/12/how-to-get-the-current-year-month-and-day-as-integers-in-python-3-using-the-datetime-module/
# REFERENCE: https://www.w3schools.com/python/python_datetime.asp
year = now.year
month = now.month
day = now.day
print("One month ago...")
one_month_ago = date(year, month - 1, day)
print(one_month_ago)
print("One year ago...")
one_year_ago = date(year - 1, month, day)
print(one_year_ago)
print("Two years ago...")
two_years_ago = date(year - 2, month, day)
print(two_years_ago)
view raw date_playground.py hosted with ❤ by GitHub
 

topherPedersen