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 the current date and time in Python script with multiple options.
Access to command line window
- / terminal
- Preferred text editor (in this case nano)
User account with root or sudo privileges Python installed
Get current date and time in Python
Use the command line to create and access a new file:
sudo nano python_date.py
The system will tell you that the file does not exist and ask you to create it. Click Yes and add the following in the text editor
: from datetime import date today = date.today() print(“Today’s date:”, today)
Save the file, and then exit. Run the file by entering
: python python_date.py
The result will display today’s date using the datetime module: Options for the datetime
Python has a number of options for configuring how the date and time are formatted.
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, date and time on
three separate lines
. Use strftime() to display time and date
The strftime() method returns a string that displays the date and time using the date, time, or datetime object.
Enter the following command in the 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 to be displayed by the system.
To display the time in 24-hour format, type: import time print (time.strftime(“%H:%M:%S”)) This example image shows the file
when nano is used in a Debian distribution: Save
and close the file.
Run the script by typing
: python python_time.py
The output displays
the time in the requested format:
To display the time in
a 12-hour format, Edit
the file for: import time print (time.strftime(“%I:%M:%S”))
Save and close
Run the script by typing
: python python_time.py Additional options
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 output in Debian is as follows:
The strftime method has many formatting options. All available options are in the official documentation.
This guide has shown you how to use Python to display the date and time. Python is fairly straightforward and offers multiple options (%X values) to display the date and time in different formats.
You now understand some basics and can try creating more complex scripts.