- Get Current Date
- Get Current Date and Time
- Create and set a Date
- Create and set a Date and Time
- Date String Formats
- Output a Formatted String from a DateTime object
- Output a Date from a Formatted String
- Get the difference between 2 dates
- Add days to a date to calculate a future date
- Handle time zones
The DateTime module is useful for handling dates in Python.
1) Get Current Date
from datetime import date
#Get Current Date
today = date.today()
print(today)

2) Get Current Date and Time
from datetime import datetime
#Get Current Date and Time
now = datetime.now()
print(now)

3) Create and set a Date
from datetime import date
#Get Current Date
mydate = date(2019,2,8)
print(mydate)

4) Create and set a Date and Time
from datetime import datetime
#Get Current Date and Time
now = datetime(2019,2,8,18,30,0)
print(now)

5) Date String Formats
Code . | Definition | Example |
%a | Weekday abbreviated name. | Mon |
%A | Weekday full name. | Monday |
%w | Weekday as a number, 0 is Sunday, 1 is Monday, 2 is Tuesday, 3 is Wednesday, 4 is Thursday, 5 is Friday, 6 is Saturday | 1 |
%d | Day of the month as a zero-padded decimal number. | 30 |
%-d | Day of the month as a number. | 30 |
%b | Month as abbreviated name. | Sep |
%B | Month as full name. | September |
%m | Month as a zero-padded decimal number. | 9 |
%-m | Month as a number | 9 |
%y | Year without century as a zero-padded number. | 13 |
%Y | Year with century as a number. | 2013 |
%H | Hour (24-hour clock) as a zero-padded decimal number. | 7 |
%-H | Hour (24-hour clock) as a number | 7 |
%I | Hour (12-hour clock) as a zero-padded number. | 7 |
%-I | Hour (12-hour clock) as a number. | 7 |
%p | Locale’s equivalent of either AM or PM. | AM |
%M | Minute as a zero-padded number. | 6 |
%-M | Minute as a number | 6 |
%S | Second as a zero-padded number. | 5 |
6) Output a Formatted String from a DateTime object
Use the strftime() method from datetime to create a formatted string from a datetime object.
from datetime import datetime
#Output a formatted string from a datetime object
now = datetime(2019,2,8,18,30,0)
print(now.strftime("%m/%d/%Y"))

7) Output a Date from a Formatted String
Use the strptime() method from datetime to create a datetime from a formatted string.
from datetime import datetime
#Convert a formatted string to a datetime object
datestring = "2019-02-19"
print(datetime.strptime(datestring,"%Y-%m-%d"))

8) Get the difference between 2 dates
Use the “-” operator to take the difference between 2 dates.
Outputs timedelta object.
from datetime import datetime,date
today = date(2019,2,8)
futuredate = date(2019,11,9)
diff = futuredate - today
print(diff)

9) Add days to a date to calculate a future date
from datetime import date,timedelta
#add 100 days to todays date
t1 = timedelta(days = 100)
today = date.today()
futuredate = today + t1
print(futuredate)

10) Handle time zones
Use the pytz module to handle time zones.
from datetime import datetime
import pytz
#Get local time in London Time
local = datetime.now()
print("Local:", local.strftime("%m/%d/%Y, %H:%M:%S"))
tz_London = pytz.timezone('Europe/London')
datetime_London = datetime.now(tz_London)
print("London:", datetime_London.strftime("%m/%d/%Y, %H:%M:%S"))

- Python Lesson 1: Hello World in Python
- Python Lesson 2: Reserved Keywords
- Python Lesson 3: Creating Variables
- Python Lesson 4: Performing math calculations in Python using operators
- Python Lesson 5: Working with Strings
- Python Lesson 6: Useful String Methods
- Python Lesson 7: Getting User Keyboard input
- Python Lesson 8: Importing Modules into your Python code
- Python Lesson 9: Decision making in Python
- Python Lesson 10: Creating loops in Python
- Python Lesson 11: Lists in Python
- Python Lesson 12: Tuples in Python
- Python Lesson 13: Dictionaries in Python
- Python Lesson 14: Sets in Python
- Python Lesson 15: Dealing with dates and time
- Python Lesson 16: Creating and using functions
- Python Lesson 17: Creating and using classes in Python
- Python Lesson 18: List Comprehensions
- Python Lesson 19: Lambda Functions
- Python Lesson 20: Error Handling