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:
- 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:
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:
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:
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.