Python time Module
- In this topic, we will explore time module in detail. We will learn to use different time-related functions defined in the time module with the help of examples.
- Python has a module named time to handle time-related tasks. To use functions defined in the module, we need to import the module first.
- Python has a module datetime to work with dates and times.
- import time
Get Current Date and Time
- Datetime class is definned inside the datetime module.
import datetime
dt = datetime.datetime.now()
print(dt)
Get Current Date
- We use today() method defined in the date class to get date object which contains current local date.
import datetime
dt = datetime.date.today()
print(dt)
Get Date from Timestamp
- We posted time module in python before three days where we got timestamp. And when we paste that timestamp here, we got that date.
Source Code:
from datetime import date
timestamp = date.formtimestamp(1586941435)
print("Date=",timestamp)
What is inside in the datetime module?
- We will use the dir() function to get the list of all attributes.
Source Code:
import datetime
print(dir(datetime))