Python Datetime Free PDF.2871
Python Datetime Free PDF.2871
Python Datetime
Python datetime module can be used to work with dates and times.
In this tutorial, we will learn how to use datetime module for different operations involving date, day of the week,
seconds, minutes, current time, etc.
import datetime
import datetime
print(x)
Output
2019-09-12 12:18:06.255164
The output contains the current time in the format YYYY-MM-DD HH:MM:SS.MICROS
import datetime
print(x)
Output
2019-09-12 12:18:06.255164
The output contains the current time in the format YYYY-MM-DD HH:MM:SS.MICROS which has year, month,
day, hour, minute, second and micro-second respectively.
The parameters year , month and day are mandatory while hour , minute , second ,
microsecond and timezone information tzinfo are optional. The optional parameters are by default 0 .
In the following example, you can observe how the default values for the optional parameters take effect.
import datetime
#create datetime object with year, month, day, hour, minute, second
x = datetime.datetime(2018, 5, 24, 5, 45, 34)
print(x)
#create datetime object with year, month, day, hour, minute, second, microsecond
x = datetime.datetime(2018, 5, 24, 5, 45, 34, 542136)
print(x)
Output
2018-05-24 00:00:00
2018-05-24 05:00:00
2018-05-24 05:45:00
2018-05-24 05:45:34
2018-05-24 05:45:34.542136
Conclusion
In this Python Tutorial, we learned about Python datetime library.
Python Programming
⊩ Python Tutorial
⊩ Install Python
⊩ Python Variables
⊩ Python Comments
Control Statements
⊩ Python If
⊩ Python If Else
Python String
Functions
⊩ Python Functions
Python Collections
⊩ Python List
⊩ Python Dictionary
Advanced
⊩ Python Multithreading
Useful Resources