Write A Python Script To Display The Current Date And Time

In this program let's try to display the current date and time.  Let's use the Python module datetime,  where the datetime module has a function named as now(). new() function will provide the current date and time object.

Let's format the date time object to get the current date and time and display it.

 

Write A Python Script To Display The Current Date And Time

 

 


Write A Python Script To Display The Current Date And Time

Let's import the required  Python modules using the import keyword.  we required that time module to get the current date and time.

from datetime import datetime



Let's call the function now()  to get the day time object,  use the string format of time to print the date

%y - is to print the year
%m - Is to print the month
%d -  is to print the day

time = datetime.now()

print("Today's date is ...")
print(time.strftime("%y - %m - %d"))




Let's again use the string formatting to print the current time.
%I - Is used to print the hours in 12 hour format
%M - is used to print the  minutes
%S - is used to print the second

print("Time right now is ...")
print(time.strftime("%I - %M - %S"))




Python Script
=====================


from datetime import datetime

time = datetime.now()

print("Today's date is ...")
print(time.strftime("%y - %m - %d"))

print("Time right now is ...")
print(time.strftime("%I - %M - %S"))




Output
==============
Today's date is ...
23 - 01 - 21
Time right now is ...
12 - 32 - 44




Output
================
Today's date is ...
23 - 01 - 21
Time right now is ...
12 - 33 - 34




Conclusion
==============
Execute the above Python program and note down the current date and time,  comment it down below if you have any suggestions to improve the above  python program.





Post a Comment

0 Comments