Getting Current Date by Python datetime now() Function

Explore the datetime.now() function visually. See how Python captures the current moment, providing real-time date and time information.

In this tutorial, you are going to learn how to get the current date and time by now() function of the Python datetime() module.

  • The now() function returns the current local date and time.
  • You have to Import datetime Module in order to use the now() function.

The later part of the tutorial also shows other ways of getting the current time.

How to use the now() function?

The general syntax for using the now() function:

datetime.now(tz=None)
  • Where tz argument specifies the time zone. If a value of None is given, this is like today().
  • For other than the None value, it must be an instance of the tzinfo subclass.
  • You may learn more about tzinfo subclass here.

An example of Python now() to get current local date and time

The following example displays the current local date and time by using the now() function of the datetime.

For that, you first need to import datetime module, as shown in the example below:

# An example of getting current date

import datetime

currDate = datetime.datetime.now()

print(currDate)

Output:

“It should display the current date and time of the system”

The example of using a time zone argument

This example uses the time zone argument in the datetime.now() function and displays the time in UTC (Universal Time Coordinated):

# Current time as UTC

import datetime

dt_utc = datetime.datetime.now(datetime.timezone.utc)

print(dt_utc)

Getting date only by now() function

In the above examples, we get the date and time while using the now() function. For only getting the current date, you may use the date() method as follows:

# Current date only demo

import datetime

date_only = datetime.datetime.now().date()

print(date_only)

This should display the current date only.

How to get time only by now() function?

Similarly, by using the time() function, you may get the time only:

# Current time only demo

import datetime

time_only = datetime.datetime.now().time()

print(time_only)

Only getting time in UTC time zone

The example below displays the time in the UTC zone without date:

#Time only in UTC demo

import datetime


time_utc = datetime.datetime.now(datetime.timezone.utc).time()

print(time_utc)

The example of getting date by using the time module

You may use the time() module as well to get the date and time for local time, GMT etc.

The example below shows using the time module where I used strftime and localtime() functions for getting the local date and formatting as shown below:

#Time module demo

from time import strftime,localtime


date_local = strftime("%A, %d %b %Y", localtime())

print(date_local)

The output of the above example should be current local time in this formation:

Tuesday, 14 Mar 2017

Getting GMT time using strftime and gmttime functions

Similarly, you may get the GMT time by using gmttime() function in the strftime() function as follows:

#Time module demo

from time import strftime,gmtime

dt_gmt = strftime("%Y-%m-%d %H:%M:%S", gmtime())

print(dt_gmt)

The output should be GMT time like this:

2018-12-04 09:41:09

You can see, this time we got the time as well as the date. You may use the formatting directives as per the requirements.

You can see the list of directives here.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!