Created
July 23, 2014 15:56
-
-
Save sankars/df9683d06d1cb663fe8e to your computer and use it in GitHub Desktop.
Date related operations in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| def main(): | |
| ## Parse string to date | |
| some_date = '2012-05-30 23:00:00' | |
| date1 = datetime.datetime.strptime(some_date, '%Y-%m-%d %H:%M:%S') | |
| print ('date1 => ' + str(date1)) | |
| date2 = datetime.datetime.now() | |
| print ('Now => ' + date2.__str__()) | |
| ## Date Manipulation | |
| delta = date2 - date1 | |
| print('delta = > ' + delta.__str__()) | |
| add_10days_date1 = date1 + datetime.timedelta(days=10) | |
| print('after 10 days date1 => ' + add_10days_date1.__str__()) | |
| subtract_10days_date1 = date1 + datetime.timedelta(days=-10) | |
| print('before 10 days date1 => ' + subtract_10days_date1.__str__()) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment