Browse Source

Date examples

George Jones 1 year ago
parent
commit
ec3808f6d4
1 changed files with 27 additions and 0 deletions
  1. 27 0
      home/public/snippits/python/dates.py

+ 27 - 0
home/public/snippits/python/dates.py

@@ -0,0 +1,27 @@
+#! /usr/bin/env python
+# Date manipulation examples
+
+# https://datagy.io/python-string-to-date/
+# https://stackoverflow.com/questions/12566152/python-x-days-ago-to-datetime
+
+from datetime import datetime
+from dateutil.relativedelta import relativedelta
+
+
+date_string = '2021-12-31'
+then = datetime.strptime(date_string, '%Y-%m-%d')
+print(f"then: {then}")
+
+now = datetime.now()
+print(f"now: {now}")
+
+diff = now - then
+print(diff)
+
+# minus 1 year
+
+oneYearAgoDate = datetime.now() - relativedelta(years=1)
+
+oneYearAgoString = oneYearAgoDate.strftime('%Y-%m-%d')
+
+print(oneYearAgoString)