Datetime Module
What You'll Learn: In this tutorial, you'll discover how to work with dates in Python using the datetime module. Dates aren't a data type of their own in Python, so we use this module to create and manipulate date objects.
Importing the datetime Module and Displaying the Current Date
You can use the datetime module to get the current date and time.
Example Code:
import datetime
x = datetime.datetime.now()
print(x)
Output:
2024-11-19 13:04:55.684672
Display Year and Weekday
The datetime module has many methods to return information about the date object.
Example Code:
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
Output:
2024
Tuesday
Creating Date Objects
To create a date, use the datetime() class (constructor) of the datetime module. You need to provide the year, month, and day.
Example Code:
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
Using the strftime() Method
The strftime() method formats date objects into readable strings. It takes one parameter, format, to specify the format of the returned string.
Example Code:
import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))
Output:
June
Reference of Format Codes
Here are some common format codes you can use with the strftime() method:
| Directive | Description | Example | Try it |
|---|---|---|---|
| %a | Weekday, short version | Wed | |
| %A | Weekday, full version | Wednesday | |
| %w | Weekday as a number 0-6, 0 is Sunday | 3 | |
| %d | Day of month 01-31 | 31 | |
| %b | Month name, short version | Dec | |
| %B | Month name, full version | December | |
| %m | Month as a number 01-12 | 12 | |
| %y | Year, short version, without century | 18 | |
| %Y | Year, full version | 2018 | |
| %H | Hour 00-23 | 17 | |
| %I | Hour 00-12 | 05 | |
| %p | AM/PM | PM | |
| %M | Minute 00-59 | 41 | |
| %S | Second 00-59 | 08 | |
| %f | Microsecond 000000-999999 | 548513 | |
| %z | UTC offset | +0100 | |
| %Z | Timezone | CST | |
| %j | Day number of year 001-366 | 365 | |
| %U | Week number of year, Sunday as the first day of week, 00-53 | 52 | |
| %W | Week number of year, Monday as the first day of week, 00-53 | 52 | |
| %c | Local version of date and time | Mon Dec 31 17:41:00 2018 | |
| %C | Century | 20 | |
| %x | Local version of date | 12/31/18 | |
| %X | Local version of time | 17:41:00 | |
| %% | A % character | % | |
| %G | ISO 8601 year | 2018 | |
| %u | ISO 8601 weekday (1-7) | 1 | |
| %V | ISO 8601 weeknumber (01-53) | 01 |
Try It Yourself: Fun Exercises
- Print Today's Date:
- Use the
datetimemodule to print today's date and time.
- Use the
- Formatted Date Strings:
- Create a date object for your birthday and print it in a different format using the
strftime()method.
- Create a date object for your birthday and print it in a different format using the
Summary:
In this Python tutorial, we learned how to work with dates using the datetime module. We explored how to get the current date and time, create date objects, and format dates into readable strings. Keep experimenting and have fun with dates in Python!