Datetime In Python
Code Example
Example 1:
>>> import datetime
>>> today = datetime.datetime.now()
>>> print(today)
2020-09-06 16:39:12.862160
>>>print(today.year,'-', today.month, '-', today.day)
2020 - 9 - 6
Example 2:
>>>import datetime
>>> old_date = datetime.date(2020, 10, 20)
>>> print(old_date)
2020-10-20
Example 3:
>>>from datetime import datetime, timedelta
>>>today = datetime.now()
>>>print(today)
2020-09-06 16:45:32.176510
>>>date_after_30_days = today + timedelta(days=30)
>>> print(date_after_30_days)
2020-10-06 16:45:32.176510
>>> date_before_60_days = today - timedelta(days=60)
>>> print(date_before_60_days)
2020-07-08 16:45:32.176510
Example 4:
# Local Datetime
>>> import datetime
>>> import pytz
>>>current_time = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))
>>>print(current_time)
2020-09-06 16:51:38.540727+05:30
>>> current_time = datetime.datetime.now(pytz.timezone('UTC'))
>>> print(current_time)
2020-09-06 11:22:58.602013+00:00
Enjoy Edge-cutting Technology 😉
Comments
Post a Comment