DevOps

How to Get the Current Date and Time in Python

logo ebook

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.

Next, find out how to set up time synchronization on Debian.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !