How to Get the Current Date and Time in Python?

Photo of author

By admin

When you are working on a program, how do you get the current time and date? The answer is simple. You use a Data type, and it gets your current date and time in a matter of seconds. But, it’s not how it works for Python.

Then how to get the current date and time in Python? In Python, if you want to get the date and time, there is no data type for it. There is a separate module called the datetime module. To get the current date and time, you have to use it. Getting the current date and time is easy in Python and can be performed using several functions provided that you have the module.

For working with time and date, there are different date objects. The date is returned in several formats, including year, day, month, minutes, hours, and seconds. This function requires the year, day, and month values and returns the output in the desired format.

This guide will help you learn how to get the current date and time in Python using the datetime module. You will learn how you can use several different functions to get the date and time.

Prerequisites To Get Current Date And Time

What you need here is-

  • Root user with sudo privileges
  • Access to command line window
  • A text editor such as nano

How To Get Current Date And Time In Python Using Different Functions

Now you will learn how to get the current date and time in Python using different functions as there are several ways to get it.

Using today() function

  1. Create and access a new file with – sudo nano python_date.py
  2. Since you don’t have the file, you are asked to create it. So click yes and type:

from datetime import date

today = date.today()

print(“Today’s date:”, today)

  1. Save and exit. Now run the file with – python python_date.py
  2. It will return the result that will look like – python python_date.py

( ‘Todays date: ‘ , ‘datetime.date (2021.06.29))

It picks up today’s date and returns it in yyyy-mm-dd format.

Using strftime()

It returns the output using date, time, and datetime objects.

Getting current time using strftime()
  1. Enter the command – sudo nano python_time.py
  2. Use this file, and define how you want the time to display-

import time

print (time.strftime(“%H:%M:%S”))

  1. Save the file and run the following command to get the current time-

python python_time.py

  1. The result will look like-

python python_time.py

17:15:23

  1. If you want the result to display in a 12-hour format, you must change the command as-

import time

print (time.strftime(“%I:%M:%S”))

Then execute the command normally and the result will be displayed in a 12 hour format.

Getting Current Date And Time Using now() function

Another part of the datetime module is now() function which produces the date and time in yyyy-mm-dd hh:mm:ss ms format.

Getting current date and time

from datetime import datetime

current_date = datetime.now()

print(“Current Date and Time=”, current_date)

string = current_date.strftime(“%d/%m/%Y %H:%M:%S”)

print(“Current Date and Time in different format=”, string)

The result will be something like-

Current Date and Time= 2021-06-29 17:15:29.633526

Current Date and Time in different format= 29/06/2021 17:15:29

Getting Current Time

from datetime import datetime

current_time = datetime.now().time()

print(“Current Time:”, current_time)

The result will look like-

Current Date: 17:16:39.372822

Formatting The Date And Time Format

You can also define datetime formatting and ask the system to display the date and time In a particular format.

Using Datetime Object

  1. Type the following to create a sample file-

sudo nano sample_format.py

  1. Now edit the file-

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))

  1. Save and exit. Run the following command-

python sample_format.py

  1. You will get the answer in three different lines as per the defined format.

python sample_format.py

Current date and time = 2021.06.29 17.15.29.563178

Today’s date – 29/6/2021

The time is now – 17.15.29

Using strftime() function

Here, today() function is used to store date in variable and strftime() is used to print date in different formats-

from datetime import date

current_date = date.today()

date1 = current_date.strftime(“%d/%m/%Y”)

print(“Date in date/month/year format =”, date1)

date2 = current_date.strftime(“%B %d, %Y”)

print(“Date in month/day/year format =”, date2)

date3 = current_date.strftime(“%m/%d/%y”)

print(“Date in month/date/year format =”, date3)

date4 = current_date.strftime(“%b-%d-%Y”)

print(“Date in month/date/year format =”, date4)

It will display the output as-

Date in Date/Month/Year format = 18/01/2021

Date in Month Name Date, Year format = January 18, 2021

Date in Month/Date/Year format = 01/18/21

Date in Month-Date-Year format = Jan-18-2021

Using datetime.now() function

Using tz parameter, you can get current time and date of a particular timezone, which Greenwich meridian by default-

# Python3 code to demonstrate

# Getting current date and time using

# now().

# importing datetime module for now()

import datetime

# using now() to get current time

current_time = datetime.datetime.now()

# Printing value of now.

print (“Time now at greenwich meridian is : ” , end = “”)

print (current_time)

It will return the output as-

Time now at greenwich meridian is : 2021-06-29 17:16:58.967356

The now() function as different attributes which it can display-

# Python3 code to demonstrate

# attributes of now()

# importing datetime module for now()

import datetime

# using now() to get current time

current_time = datetime.datetime.now()

# Printing attributes of now().

print (“The attributes of now() are : “)

print (“Year : “, end = “”)

print (current_time.year)

print (“Month : “, end = “”)

print (current_time.month)

print (“Day : “, end = “”)

print (current_time.day)

print (“Hour : “, end = “”)

print (current_time.hour)

print (“Minute : “, end = “”)

print (current_time.minute)

print (“Second : “, end = “”)

print (current_time.second)

print (“Microsecond : “, end = “”)

print (current_time.microsecond)

The output will display the attributes as-

The attributes of now() are :

Year : 2021

Month : 06

Day : 29

Hour : 17

Minute : 18

Second : 12

Microsecond : 292616

Getting Time of Your Timezone

You can define the code to get the date and time of your timezone. The pytz library has timezones defined and now() takes input of timezone and gets you the current date and time of your output.

# Python3 code to demonstrate

# attributes of now() for timezone

# for now()

import datetime

# for timezone()

import pytz

# using now() to get current time

current_time = datetime.datetime.now(pytz.timezone(‘Asia/Kolkata’))

# printing current time in India

print (“The current time in India is : “)

print (current_time)

The result will look like-

The current time in India is :

2021-06-29 17:58:23.973616+05:30

Final Words

Python is a very interesting programming language. The functions are amazing and you can enjoy managing different combinations using different functions. Getting the current date and time is one of the simplest functions but there are many ways to get it. In this guide, you get various answers to how to get the current date and time in Python.

Using today(), strftime(), and now(), you will learn how to obtain the current date and time. Using the above functions, you can get results in different formats. You also learned how to get the current date and time of your timezone. Each date and time format combination can be made in any combination you wish, so there is no limit to how many you can try out.

Leave a Comment