
Overview
A date in Python is not a data type of its own, but a module called datetime can be imported to work with dates as date objects.
Importing the datetime Module for Use
import datetime
x = datetime.datetime.now() #get the current date and time
print(x)
Output:
2025-04-04 10:42:55.315652
Note
The result “2025-04-04 10:42:55.315652” contains the year, month, day, hour, minute, second, and microsecond.
Creating a Date Object
To create a date, the datetime() class of the datetime module is used.
The datetime() class requires three parameters to create a date: year, month, day.
import datetime
x = datetime.datetime(2025, 11, 22)
print(x)
Output:
2025-11-22 00:00:00
Note
The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone).
Using the strftime() Method
The datetime object has a strftime() method for formatting date objects into readable strings. It takes one parameter (format) to specify the format of the returned string.
import datetime
x = datetime.datetime(2025, 4, 15)
print(x.strftime("%B"))
Output:
April
Parameters
Format Code | Description | Example |
---|---|---|
%a | Weekday, short version | Mon |
%A | Weekday, full version | Monday |
%w | Weekday as a number 0-6, 0 is Sunday | 4 |
%d | Day of month 01-31 | 22 |
%b | Month name, short version | Jan |
%B | Month name, full version | January |
%m | Month as a number 01-12 | 01 |
%y | Year, short version, without century | 25 |
%Y | Year, full version | 2025 |
%H | Hour 00-23 | 16 |
%I | Hour 00-12 | 5 |
%p | AM/PM | PM |
%M | Minute 00-59 | 33 |
%S | Second 00-59 | 2 |
%f | Microsecond 000000-999999 | 345223 |
%z | UTC offset | 100 |
%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 Jan 22 16:33:02 2025 |
%C | Century | 21 |
%x | Local version of date | 01/22/25 |
%X | Local version of time | 16:33:02 |
%% | A % character | % |
%G | ISO 8601 year | 2025 |
%u | ISO 8601 weekday (1-7) | 1 |
%V | ISO 8601 weeknumber (01-53) | 1 |
Python Notes:
- The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
- Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
- Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
- Python string methods return new values, and DO NOT change the original string
- Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
- Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
- As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)
- Python does not have built-in support for arrays, but Python lists can be used as pseudo “arrays”; therefore, all Python list methods will work with these pseudo “arrays”
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.