The module Python datetime facilitates the use of dates and times. This lets you display data in different formats and create date objects with just a few lines of code.
Presentation of the datetime module¶
Python datetime is a Python module that you absolutely need to know, whether you have just completed your first Python tutorial or have been working with this Internet programming language for a long time. Modules are the counterpart of code libraries in other programming languages. By importing the datetime module, you add date and time processing functions to Python. Specific date objects in the module are used for this purpose, which you can easily create in your code.
Using Python in your web project? With Deploy Now from IONOS, you can easily deploy code changes at any time via GitHub and track the current status of your project live.
Python datetime: date and time¶
For example, you can use Python datetime to store a date and time in a separate object. To do this, you need the Python datetime constructorwhose syntax looks like this:
import datetime as dt
dt.datetime(année, mois, jour, heure, minute, seconde, microseconde, fuseau horaire)
python
Parameters accepted by the function represent the current date and time. Parameters to specify the time are optional; the default is always 0. The same goes for the parameter that specifies the time zone, which defaults to NUL. The datetime function returns you a Python datetime object.
Python date: date¶
Like the datetime constructor, the constructor Python date can also be used to create a date object. The current time is irrelevant here. Syntactically, the date constructor is similar to its datetime counterpart:
import datetime as dt
dt.date(année, mois, jour)
python
The transfer parameters that the Python date constructor takes are the desired year, month, and day. The result of the function call is a date object.
Invoke the current date: the procedure to follow¶
If you do not want to save specific data, but the current date and time, predefined functions are available. Depending on whether you are interested in the current date only or in the date and time, you will have to resort to another function.
Procedure for the current date¶
If you are only interested in the date, which consists of the year, month, and day, you can use a function of the Python date class called » today « . The function call is extremely simple:
import datetime as dt
current_date = dt.date.today()
python
To call todayyou must first specify the datetime module, which is notified by thealias dt in the example above. Then use dot notation to invoke the date class, in which the today function is in turn specified. Once the above code is executed, the current_date variable contains a Python date object with the current date.
Procedure for current date and time¶
If you also need the current time, a function of the datetime class will come in handy: the now function. During a call from nowyou get a Python datetime object that contains the current date and time – and is millisecond accurate.
import datetime as dt
current_date_and_time = dt.datetime.now()
python
The call is made analogous to calling the today function, but in the datetime class. A datetime object containing the current date and time is stored in the “current_date_and_time” variable after the function call.
If you are only interested in the individual values of an object, such as the day, you can use dot notation to invoke exactly that value. « current_date.day » allows you to invoke the current day for example.
Format data in Python¶
Especially for applications where you want to output the date, you need to have a way to put datetime or date objects in the correct format for your application. The Python datetime module also offers a function called strftime for this purpose.
The function syntax looks like this:
import datetime as dt
current_date = dt.date.today()
current_date.strftime(format)
python
The function is called directly on a date or datetime object and accepts a specific format as a transfer parameter. You must specify this format as a Python string, so in quotes. Python supports a number of different formats, the abbreviations of which are shown in the following table:
Abbreviation | Description | Example |
---|---|---|
%To | Day of the week, abbreviated notation | Sat |
%AT | Day of the week, fully written | saturday |
%w | Day of the week as a number | 6 |
%u | Day of the week in ISO 8601 format | 6 |
%j | Current day number (compared to the whole year) | 365 |
%b | Name of the month, abbreviated notation | Dec |
%B | Name of the month, fully written | December |
%m | Month as number | 12 |
%U | Week number, starting Sunday as the first day of the week | 50 |
%W | Week number, starting Monday as the first day of the week | 50 |
%y | Year, abbreviated notation | 22 |
%Y | Year, fully written | 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 | Offset from Coordinated Universal Time | +0100 |
%Z | Time zone | CST |