How to Get the Current Date and Time in Python

August 14, 2019

Introduction

Python is a versatile programming language used to develop desktop and web applications. It allows you to work on complex projects.

Learn how to get current date and time in the python script with multiple options.

How to get the current date and time using Python.

Prerequisites

  • Command line / terminal window access
  • User account with root or sudo privileges
  • Python installed
  • Prefered text editor (in this case nano)

Get Current Date Time in Python with datetime Module

Use the command line to create and access a new file:

sudo nano python_date.py

The system will tell you the file does not exist and ask you to create it. Click Yes and add the following in your text editor:

from datetime import date 
today = date.today()
print("Today's date:", today)

Save the file, then exit. Run the file by entering:

python python_date.py

The result will show today’s date using the datetime module:

Date retrieved from the datetime module.

Options for datetime formating

Python has a number of options to configure the way date and time are formated.

Create a sample file:

sudo nano sample_format.py

Edit the file as follows:

import datetime

e = datetime.datetime.now()

print ("Current date and time = %s" % e)

print ("Today's date:  = %s/%s/%s" % (e.day, e.month, e.year))

print ("The time is now: = %s:%s:%s" % (e.hour, e.minute, e.second))

Save the file and exit. Run the file by entering:

python sample_format.py

The system displays the date and time, the date, and the time on three separate lines.

Information displayed in requested format.

Use strftime() to display Time and Date

The strftime() method returns a string displaying date and time using date, time or datetime object.

Enter the following command in your terminal window:

sudo nano python_time.py

You have created a new file named python_time.py. Use this file to define the format of the information the system is to display.

To display the time in 24-hour format, enter:

import time
print (time.strftime("%H:%M:%S"))

This example image shows the file when using nano on a Debian distribution :

Image displays the code in python_time.py file.

Save and close the file.

Execute the script by typing:

python python_time.py

The result displays the time in the requested format:

current time in 24 hour format in python

To display the time in a 12-hour format, edit the file to:

import time

print (time.strftime("%I:%M:%S"))

Save and close the file.

Execute the script by typing:

python python_time.py

Additional Options Using strftime

The strftime method accepts several formatting options.

First, create a new sample file:

sudo nano test_format.py

Edit the file as follows:

import datetime

e = datetime.datetime.now()

print (e.strftime("%Y-%m-%d %H:%M:%S"))

print (e.strftime("%d/%m/%Y"))

print (e.strftime("%I:%M:%S %p"))

print (e.strftime("%a, %b %d, %Y"))

Save the file and exit.

Run the file by typing:

python test_format.py

An example of the result in Debian is below:

Example output of using the strftime method

The strftime method has many formatting options. All the available options are in the official documentation.

Conclusion

This guide has shown you how to use Python to display the date and time. Python is pretty straightforward and offers multiple options (%X values) to show the date and time in different formats.

You now understand some basic concepts and can attempt to create more complex scripts.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Install Latest Version Of Python 3 on CentOS 7
March 12, 2019

Python is a popular, stable, and well-performing programming language. CentOS 7 uses Python 2.7.5, but as...
Read more
How To Install PIP on Debian 9
April 10, 2019

Pip means "preferred installer program" or "Pip installs packages." It simplifies the installation and...
Read more
How To Install Python 3 on Windows 10
April 2, 2019

Unlike most Linux distributions, Windows does not come with the Python programming language by default...
Read more
How to Check Python Version in Linux, Mac, & Windows
October 1, 2019

Python is a popular programming language with different versions organized by release date...
Read more