How to use Python datetime
The Python datetime format makes it easy to work with times and dates. With just a few lines of code, you can create dates and times in different formats.
Python datetime format at a glance
Whether you’ve just finished reading your first Python tutorial or have already been working with web programming languages for some time now, Python datetime is a module you need to know. You can think of modules as being similar to code libraries in other programming languages. By importing the datetime module, you can add date and time processing functions to Python. Within the module, you can use specific date objects, which can easily be created in your code.
Using Python for your web project? With IONOS Deploy Now, you can see the current status of your project and deploy updates at any time using GitHub.
Python datetime for times and dates
You can use Python datetime to save a date and time in a separate object. To do so, you need the Python datetime constructor, which uses the following syntax:
import datetime as dt
dt.datetime(year, month, day, hour, minute, second, microsecond, timezone)
pythonThe parameters used by the function represent the current date and time. The parameters used to specify the time are optional with the default value being set at 0. The same applies to the time zone parameters, which also has a default value of 0. The datetime function returns a Python datetime object.
Python date for dates
As with the datetime constructor, the Python date constructor allows you to create a date object. In this instance, the time is irrelevant. The syntax is similar in both the datetime and date constructor.
import datetime as dt
dt.date(year, month, day)
pythonThe parameters used by the Python date constructor include the year, month and day. The result of this is a date object.
How to get the current date
If you want to use the current date and time rather than specific ones, you can usepredefined options. There are different options depending on whether you just want to use the current date or you want to use both the date and time.
Current date
If you want to use a date with the year, month and day, you can use the Python date class ‘today’. The execution is simple:
import datetime as dt
current_date = dt.date.today()
pythonTo execute the today function you need to enter the datetime module which you can see in the example above with the alias dt. Then use the dot notation to access the date class where the today function is specified. Once the above-mentioned code has been executed the variable current_date will contain a Python date object with the current date.
Current date and time
If you also need to include the time, you can use the now function, By executing now, you will get a Python datetime object that contains the current time alongside the current date. This will be precise down to the millisecond.
import datetime as dt
current_date_and_time = dt.datetime.now()
pythonThe execution is the same as the today function, however, it occurs in the datetime class. In the ‘current_date_and_time’ variable, once the function has been executed a datetime object is created that contains the current date and time.
If you only want to see an individual value of an object, such as the day, you can use dot notation to get this value. Use ‘current_date.day’ to display the current day only.
How to format dates in Python
If you are working on an application that uses dates, you’ll need to be able to format the datetime or date objects in the right way. The Python datetime module has a function called strftime to do this.
The syntax is as follows:
import datetime as dt
current_date = dt.date.today()
current_date.strftime(format)
pythonThe function is executed directly in a date or datetime object and uses the parameters to set a specific format. This format has to be given as a Python string, which needs to be placed in quotation marks. Python supports a range of different formats. The following table provides an overview and includes their abbreviations:
Abbreviation | Description | Example |
---|---|---|
%a | Weekday, short | Sat |
%A | Weekday, long | Saturday |
%w | Weekday as a number | 6 |
%u | ISO 8601 weekday | 6 |
%j | Day count (relating to the entire year) | 365 |
%b | Month, short | Dec |
%B | Month, full | December |
%m | Month as a number | 12 |
%U | Week number, beginning with Sunday as the first workday | 50 |
%W | Week number, beginning with Monday as the first workday | 50 |
%y | Year, short | 22 |
%Y | Year, full | 2022 |
%H | Hour (0-23) | 22 |
%I | Hour (0-12) | 11 |
%p | AM or PM | AM |
%M | Minute | 59 |
%S | Second | 37 |
%f | Microsecond | 567213 |
%z | UTC offset | +0100 |
%Z | Time zone | CST |