Python Program to Print Date and Time

In this article, I've created multiple programs in Python, to print date and time in different-different ways. Here are the list of programs included in this article:

Program to Print Current Date and Time in Python

The question is, write a Python program to print the current date and time. The program given below is its answer:

import datetime

current_dt = datetime.datetime.now()
print("The Current Date and Time is:", current_dt)

The snapshot given below shows the sample output produced by above Python program, on printing the current date and time, in default format:

python program print current date time

Program to Print Today's Day Name in Python

This program prints the name of today's day, using now() method of datetime module.

import datetime
dt = datetime.datetime.now()
print("Today is", dt.strftime("%A"))

The output produced by above program, will be the name of the current day such as Monday, Sunday, Tuesday, etc.

Program to Print Today's Date in Python

This program prints the current date.

import datetime
dt = datetime.datetime.now()
print("Date:", dt.strftime("%x"))

The output produced by this program looks like:

Date: 11/08/21

where 11 is the month number, that is November, 08 refers that, today is the 8th day of the month, whereas 21 is the year, that is the short version 2021.

Program to Print Current Time in Python

Here is another program to print current time separately.

import datetime
dt = datetime.datetime.now()
print("Time:", dt.strftime("%X"))

This program produces output like:

Time: 09:02:56

The character is same, but in previous program, it was %x (lowercase 'x'), but in this program, it is %X (uppercase 'X')

Program to Print Date and Time in Specified Format

This is the last Python program of this article, created to print all the things at once, and of course, in manual or specified format.

import datetime

dt = datetime.datetime.now()

print("----The Current Date and Time----")
print(dt.strftime("%a"))
print(dt.strftime("%I"), ":", dt.strftime("%M"), " ", dt.strftime("%p"), sep="")
print(dt.strftime("%d"), dt.strftime("%b"), dt.strftime("%y"), sep="-")

The output produced by above program, looks like:

python program print date time

Note - To learn the topic in detail, refer to its separate tutorial.

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »


Liked this post? Share it!