Skip to content

Instantly share code, notes, and snippets.

@ahmedshahriar
Created November 26, 2020 17:07
Show Gist options
  • Save ahmedshahriar/b49f4ef8d741b90314f5fb96f6dc561d to your computer and use it in GitHub Desktop.
Save ahmedshahriar/b49f4ef8d741b90314f5fb96f6dc561d to your computer and use it in GitHub Desktop.
"""
code snippet to add/subtract datetime object
https://github.com/ahmedshahriar
"""
from datetime import datetime, timedelta
# addition
d = datetime.now() + timedelta(days=1, weeks=1, hours=19,seconds=1,milliseconds=1,microseconds=1)
#subtraction
d = datetime.now() - timedelta(days=1, weeks=1, hours=19,seconds=1,milliseconds=1,microseconds=1)
d.strftime('%Y-%m-%d %H:%M:%S')
# output sample
# 2020-12-05 12:05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment