25 datetime module in python
----- datetime to work with dates as date objects.
----Example----
Import the datetime module and display the current date:
import datetime
x = datetime.datetime.now()
print(x)
----When we execute the code from the example above the result will be:
2021-10-02 21:34:06.726296
The date contains year, month, day, hour, minute, second, and microsecond.
-----Example
Return the year and name of weekday:
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
-------Creating Date Objects-------
To create a date, we can use
the datetime() class (constructor) of the datetime module.
-----Example------
Create a date object:
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
strftime(), and takes one parameter, format,
to specify the format of the returned string:
Example
Display the name of the month:
import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))
Comments
Post a Comment