Python Date and time

How to get Current Date and Time in Python?

Python is a modern programming language that is adaptable, general-purpose, and high-level. It also has datetime and time modules for performing time-sensitive activities. The time value begins on January 1st, 1970, and most operating systems support it until 2038. A user may require the current system date and time for a variety of actions. In Python, there are numerous ways to obtain the current date and time by utilizing built-in and third-party modules. This is where the Python datetime module comes in handy. In this article, we will learn more about the Python datetime module, how to get the current date and time along with programming examples.

Python Date and Time

Date and time are not data types in Python, however, a module called datetime can be imported to work with both the date and the time. There is no need to install the datetime module outside because it is built into Python. In Python, you must utilize the datetime (high-level Object-oriented interface to dates and times) module. It includes classes that let you to manipulate dates and times in both simple and advanced ways. Let us look at the various methods we can use to get Python datetime.

Methods to get datetime

There are three techniques that are used for obtaining the datetime in Python. They are described as follows:

  • By making use of the datetime module
  • By employing the pendulum module
  • Using the pytz module

Example 1: Python get today’s date

The date class is imported from the datetime module in this case. Then, to obtain the current local date, we utilized the date.today() method.

Example

                    

# importing date module
from datetime import date

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

Output

                    

Today's date: 2021-09-28

Example 2: Current date in different formats

We can use the strftime() method to generate a string that represents a date in several formats.

Example

                    

# importing date module
from datetime import date
today = date.today()

# dd/mm/yy
date1 = today.strftime('%d/%m/%Y')
print('date1 =', date1)

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

# mm/dd/yy
date3 = today.strftime('%m/%d/%Y')
print('date3 =', date3)

# Month abbrevation , day and yeat
date4 = today.strftime('%b-%d-%Y')
print('date4 =', date4)

Output

                    

date1 = 28/09/2021
date2 = September 28, 2021
date3 = 09/28/2021
date4 = Sep-28-2021

Using the Datetime Module

The datetime module is a built-in Python module. It has a plethora of built-in capabilities for obtaining the current date and time. The datetime module’s now() function returns the current time as well as the date. The datetime.now() function returns the current local time and date. It displays datetime in the YYYY-mm-dd hh:mm:ss.microseconds format by default.

To import the datetime module we using the following method:

                    

from datetime import datetime

Example 3: Get the Current Date and Time

Example

                    

# Importing datetime module
from datetime import datetime

# storing the current time in the variable
c = datetime.now()

# Displays Time
current_time = c.strftime('%H:%M:%S')
print('Current Time is:', current_time)
# OR
# Displays Date along with Time
print('Current Date and Time is:', c)

Output

                    

Current Time is: 12:15:21
Current Date and Time is: 2021-09-28 12:15:21.283077

Explanation

To obtain the current date and time, we implement the datetime.now() in the above program. Then, using the now() method, we obtained a datetime object with the current date and time. However, we simply want the current time, so we used strftime(). We then constructed a string indicating the current time using the datetime.strftime() method. We then use %H to extract hours, %M to extract minutes, and %S to extract seconds.

Using the pendulum module

The pendulum is a timezone library that makes date and time manipulation easier. The pendulum module, like the datetime module, has a now() method that retrieves the current date and time. It is comparable to the pytz module, with the sole difference being that the pendulum module is faster. But first, we need to install the pendulum module in Python by using the following command in Terminal:

                    

python3 -m pip install pendulum

Example

                    

from datetime import datetime
import pendulum

Ind = pendulum.timezone('Asia/Kolkata')
NY = pendulum.timezone('America/New_york')

print('Current Time in India:', datetime.now(Ind))
print('Current Time in New York:', datetime.now(NY))

Output

                    

Current Time in India: 2021-09-28 17:50:00.012945+05:30
Current Time in New York: 2021-09-28 08:20:00.084583-04:00

Using the pytz module

To obtain the date and time for a specific timezone, now() accepts timezone as an input and returns timezone-oriented output time. However, these time zones are defined in the pytz library.

Example

                    

import datetime
import pytz
 
time_now_zone = datetime.datetime.now(pytz.timezone("Asia/Kolkata"))
print('Current Time in India is : ', time_now_zone)
print('Set Timezone : ', time_now_zone.tzinfo)

time_now_zone = datetime.datetime.now(pytz.timezone("America/New_York"))
print('Current Time in New York is : ', time_now_zone)
print('Set Timezone : ', time_now_zone.tzinfo)

time_now_zone = datetime.datetime.now(pytz.timezone("Europe/Berlin"))
print('Current Time in Germany is : ', time_now_zone)
print('Set Timezone : ', time_now_zone.tzinfo)

Output

                    

Current Time in India is :  2021-09-28 23:37:21.912189+05:30
Set Timezone :  Asia/Kolkata

Current Time in New York is :  2021-09-28 14:07:21.913598-04:00
Set Timezone :  America/New_York

Current Time in Germany is :  2021-09-28 20:07:21.914519+02:00
Set Timezone :  Europe/Berlin

Frequently Asked Questions

Q1. What is the datetime.now() method in Python?

The Python library defines a function that may be used to obtain the current time and date. the now() function The current local date and time, as described by the datetime module, are returned. In Python, a date is not a data type in and of itself, but we may work with dates as date objects by importing the datetime module. The syntax is as follows:

                    

datetime.now(tz)

where,

tz – specified time zone of which current time and date is required

Example

                    

import datetime

# using now() to get current time
current_time = datetime.datetime.now()
print ('Current Date and Time is : ', current_time)

Output

                    

Current Date and Time is :  2021-09-28 17:54:45.502507

Q2. What does datetime.now() return?

The datetime.now() method is used to return the current date and time in the time format.

Example

                    

from datetime import datetime

# using now() to get current time
current_time = datetime.now()
print ('Current Date and Time is:', current_time)

# datetime object containing current date and time
now = datetime.now()
print('Current Date and Time is:', now)

Output

                    

Current Date and Time is: 2021-09-28 18:03:25.162270
Current Date and Time is: 2021-09-28 18:03:25.162342

Q3. How do I get the current date in Python?

To get the current date, the date class is imported from the datetime module in this case. Then, to obtain the current local date, we utilized the date.today() method. The Python Datetime module includes the Date class, which can be used to represent and manipulate dates. The Gregorian calendar is taken into account by the Date class.

  • Import the datetime module’s date class
  • To obtain the current date, use the date.today() function

Example

                    

from datetime import date

today = date.today()
print('Current Date:', today)

Output

                    

Current Date: 2021-09-28

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.