...

Web Technologies - PYTHON

Back to Course

Lesson Description


Lession - #285 Python Dates



Datetime in Python

Dates in Python

Although a date is not a data type in Python, we may use the datetime module to work with dates as date objects.

Example

Import the datetime module and use the following code to display the current date:

import datetime

x = datetime.datetime.now(>
print(x>



Output:

Sorry cannot be displayed Try it here



Date Output

When we run the code from the previous example, we get the following:
21.01.2022 11:16:47.419132
The year, month, day, hour, minute, second, and microsecond are all included in the date.
Many methods in the datetime module return information about the date object.
Here are a few samples of what you'll learn about later in this chapter:

Example
Return the weekday's year and name:

import datetime

x = datetime.datetime.now(>
print(x.year>
print(x.strftime("%A">
>


Output:

Sorry cannot be displayed Try it here

Example
The month's name should be displayed: