Skip to content

Instantly share code, notes, and snippets.

@atulkumar2
Created April 29, 2022 08:39
Show Gist options
  • Save atulkumar2/325edb4de790e857f246abd5ad4211a3 to your computer and use it in GitHub Desktop.
Save atulkumar2/325edb4de790e857f246abd5ad4211a3 to your computer and use it in GitHub Desktop.
Check some date and datetime functioning
from datetime import datetime, timedelta
date1 = datetime.now()
date2 = date1 + timedelta(days=1)
print(date1)
print(date2)
print(date1 > date2)
print(date1.date())
print(date2.date())
print('date1.date() > date2.date():', date1.date() > date2.date())
print('date1.date() < date2.date():', date1.date() < date2.date())
print('date2 - date1:', date2 - date1)
print('date2.date() - date1.date():', date2.date() - date1.date())
print('date2 - date1 type:', type(date2 - date1))
print('date2.date() - date1.date() type:', type(date2.date() - date1.date()))
print('date2 - date1 days:', (date2 - date1).days)
print('date2.date() - date1.date() days:', (date2.date() - date1.date()).days)
print('date2 - date1 days type:', type((date2 - date1).days))
print('date2.date() - date1.date() days type:', type((date2.date() - date1.date()).days))
@atulkumar2
Copy link
Author

2022-04-29 08:37:49.524254
2022-04-30 08:37:49.524254
False
2022-04-29
2022-04-30
date1.date() > date2.date(): False
date1.date() < date2.date(): True
date2 - date1: 1 day, 0:00:00
date2.date() - date1.date(): 1 day, 0:00:00
date2 - date1 type: <class 'datetime.timedelta'>
date2.date() - date1.date() type: <class 'datetime.timedelta'>
date2 - date1 days: 1
date2..date() - date1.date() days: 1
date2 - date1 days type: <class 'int'>
date2..date() - date1.date() days type: <class 'int'>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment