Using of Datetime Library in python Class 11-12 Notes
As python provides a large set of library, it also contains datetime library. This library displays the date in YYYY-MM-DD format. Python datetime module handles the extraction and formatting of date and time variables. All the attributes can be accessed using dot(.) operator with the date object.
functions in datetime module
Date functions | Description |
today( ) | used to display today’s date |
year | to display year |
month | to display month |
day | to display day |
Time functions | Description |
now() | used to display current time |
hour | to display hour |
minute | to display minute |
second | to display second |
today() Implementation:
import datetime date_today=datetime.date.today() #printing today's date print("Today date is:",date_today) #printing today's year print("We are in Year:",date_today.year) #printing today's month number print("Today is",date_today.month ,"month of the year") #printing today's day print("Today is",date_today.day,"day of the month")
Output:
Today date is: 2022-02-02 We are in Year: 2022 Today is 2 month of the year Today is 2 day of the month >>>
Screenshots:
now() Implementation:
import datetime time_now=datetime.datetime.now() #printing current date and time print("The current time is:",time_now) #printing current hour print("Current hour is",time_now.hour) #printing current minute print("Current Minute",time_now.minute) #printing current seconds print("Current seconds",time_now.second)
Output:
The current time is: 2022-02-02 21:32:32.937900 Current hour is 21 Current Minute 32 Current seconds 32 >>>