Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Created April 7, 2020 16:01
Show Gist options
  • Save topherPedersen/d107f12d4c94a139dceb04f6f7451d0a to your computer and use it in GitHub Desktop.
Save topherPedersen/d107f12d4c94a139dceb04f6f7451d0a to your computer and use it in GitHub Desktop.
Working with ISO 8601 Dates in Python Part II
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment