Python Program to Display the Current Date and Time
Beginner · Dates and times
This program shows the current date and time using Python's datetime module, and formats them in common ways, such as 18/10/2026 or Sunday, 18 October 2026. It reads the computer's clock, so the output shows the moment the program ran and is different every time.
Program
Date and time: 2026-10-19 01:59:01.044999 Date: 2026-10-19 Time: 01:59:01 Day/month/year: 19/10/2026 Full date: Monday, 19 October 2026 12-hour time: 01:59 AM Year: 2026 Month: 10 Day: 19 Hour: 1 | Minute: 59
How it works
datetime.now()reads the computer's clock and returns adatetimeobject holding the date and the time down to microseconds, so the output changes each time you run it.now.date()keeps only the date part.strftime()(string format time) builds text from codes:%dis the day,%mthe month number,%Ythe four-digit year,%Athe weekday name and%Bthe month name.%Hgives the hour on the 24-hour clock, while%Iwith%pgives the 12-hour clock with AM or PM.- Each part of the date and time is also available as a number, such as
now.yearandnow.hour.
Other ways to write it
India time from UTC
UTC time: 18-10-2026 20:29 India time: 19-10-2026 01:59 IST
datetime.now() gives the time wherever the computer is. To show one country's time, take the UTC time and convert it with astimezone(); India is 5 hours 30 minutes ahead of UTC all year.
Using the time module
Seconds since 1 January 1970: 1792355341 Local time: 19 Oct 2026, 01:59:01 AM
time.time() counts the seconds since the start of 1970, which programs use to measure how long something takes, and time.strftime() uses the same format codes.
Try it yourself
Print "Good morning", "Good afternoon" or "Good evening" depending on the value of now.hour.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises