Get current date time python

In this article, you will learn to get today's date and current date and time in Python. We will also format the date and time in different formats using strftime() method.

Video: Dates and Times in Python

There are a number of ways you can take to get the current date. We will use the date class of the datetime module to accomplish this task.


Example 1: Python get today's date

from datetime import date

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

Here, we imported the date class from the datetime module. Then, we used the date.today() method to get the current local date.

By the way, date.today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.


Example 2: Current date in different formats

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)

When you run the program, the output will be something like:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019

If you need to get the current date and time, you can use datetime class of the datetime module.

Example 3: Get the current date and time

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	

You will gate output like below.

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56

Here, we have used datetime.now() to get the current date and time. Then, we used strftime() to create a string representing date and time in another format.

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.

Get current date time 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:

Get current date time python

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.

Get current date time python

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 :

Get current date time python

Save and close the file.

Execute the script by typing:

python python_time.py

The result displays the time in the requested format:

Get current date time 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:

Get current date time python

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.

How do I get the current time in Python?

You can get the current time in python using datetime. now(). strftime("%H:%M:%S") from the DateTime module.

Which function is used to see the current date and time in Python?

Python library defines a function that can be primarily used to get the current time and datetime. now() function and return the current local date and time, which is defined under the DateTime module.

What is datetime datetime now () in Python?

Python library defines a function that can be primarily used to get current time and date. now() function Return the current local date and time, which is defined under datetime module. Parameters : tz : Specified time zone of which current time and date is required.

How do I get the current UTC time in Python?

Use datetime. datetime. datetime. utcnow() to get a datetime. datetime object representing the current UTC time.