Editor

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

current_date_time.py
from datetime import datetime
now = datetime.now() # the date and time at this moment
print("Date and time:", now)
print("Date:", now.date())
print("Time:", now.strftime("%H:%M:%S"))
print()
print("Day/month/year:", now.strftime("%d/%m/%Y"))
print("Full date:", now.strftime("%A, %d %B %Y"))
print("12-hour time:", now.strftime("%I:%M %p"))
print()
print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
print("Hour:", now.hour, "| Minute:", now.minute)
Output
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

  1. datetime.now() reads the computer's clock and returns a datetime object holding the date and the time down to microseconds, so the output changes each time you run it.
  2. now.date() keeps only the date part.
  3. strftime() (string format time) builds text from codes: %d is the day, %m the month number, %Y the four-digit year, %A the weekday name and %B the month name.
  4. %H gives the hour on the 24-hour clock, while %I with %p gives the 12-hour clock with AM or PM.
  5. Each part of the date and time is also available as a number, such as now.year and now.hour.

Other ways to write it

India time from UTC

India time from UTC
from datetime import datetime, timezone, timedelta
utc_now = datetime.now(timezone.utc)
ist = timezone(timedelta(hours=5, minutes=30), "IST")
print("UTC time: ", utc_now.strftime("%d-%m-%Y %H:%M"))
print("India time:", utc_now.astimezone(ist).strftime("%d-%m-%Y %H:%M %Z"))
Output
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

Using the time module
import time
print("Seconds since 1 January 1970:", round(time.time()))
print("Local time:", time.strftime("%d %b %Y, %I:%M:%S %p"))
Output
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.

All Python programs · CBSE class 12 practical file · Python exercises