Current date and time in Python
How to get the current date and time in Python?
In this article, TipsMake.com will show you how to display the current date and time in Python. We will also use the strftime function () to specify the date and time in different formats.
There are many different ways to do this. In this article we will apply class date and class time in the datetime module.

Example 1: Displays the current date in Python
from datetime import date today = date.today() print("Ngay hien tai:", today)
Result:
Ngay hien tai: 2019-07-11
Here, we import the date class from the datetime module and then use the date.today () method to retrieve the current local date.
Next, we use today
variable as the date object and use the strftime () method to create a string that displays dates in different formats from this object.
Example 2: Display the current date in different formats
from datetime import date today = date.today() # dd/mm/YY d1 = today.strftime("%d/%m/%Y") print("d1 =", d1) # Tháng viết đầy đủ # Viết bởi TipsMake.com d2 = today.strftime("%B %d, %Y") print("d2 =", d2) # mm/dd/y d3 = today.strftime("%m/%d/%y") print("d3 =", d3) # Tháng viết tắt d4 = today.strftime("%b-%d-%Y") print("d4 =", d4)
Run the program, the results returned in the following form:
d1 = 11/07/2019 d2 = July 11, 2019 d3 = 07/11/19 d4 = Jul-11-2019
If you want to display both the current date and time, you can use the class datetime of the datetime module .
Example 3: Displays the current date and time in Python
from datetime import datetime # đối tượng datetime chứa ngày và giờ hiện tại # viết bởi TipsMake.com now = datetime.now() print("now =", now) # dd/mm/YY H:M:S dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print("Ngay va gio hien tai =", dt_string)
Result:
now = 2019-07-11 17:15:44.166750 Ngay va gio hien tai = 11/07/2019 17:15:44
The example above uses datetime.now () to retrieve the current date and time. Then use the strftime method () to create a string representing the date and time in another format.
Example 4: Displays the current time in Python
from datetime import datetime now = datetime.now() current_time = now.strftime("%H:%M:%S") print("Gio hien tai =", current_time)
Result:
Gio hien tai = 17:31:58
Here, we import the datetime class from the datetime module and then use the now () method to get the datetime object containing the current date and time.
Next, we use the datetime.strftime () method to create a string that displays the current time.
If you need to create a time object containing the current time, you can do the following:
from datetime import datetime now = datetime.now().time() # time object print("Thoi gian hien tai:", now) print("type(now) =", type(now))
Results returned:
Thoi gian hien tai: 09:37:48.952057 type(now) =
Example 5: Displays the current time using the time module
You can also retrieve the current time using the time module.
import time t = time.localtime() current_time = time.strftime("%H:%M:%S", t) print(current_time)
Previous article: strptime () function in Python
Next lesson: Convert the timestamp value in Python
You should read it
May be interested
- Convert the timestamp value in Pythoninstructions to learn how to convert timestamp values into datetime objects and datetime objects into timestamp values in python
- Strptime () function in Pythonthis article shows you how to create a datetime object (date, time, and time) from the corresponding string.
- Strftime () function in Pythonthis article shows you how to convert date, time, and time objects into strings corresponding to a given format.
- The function id () in Pythonthe built-in function id () in python returns a single integer value that identifies an object.
- The ord () function in Pythonthe built-in ord () function in python returns an integer representing the unicode code of the specified character.
- The input () function in Pythonpython's built-in input () function allows users to enter data, convert it into a string, and return the entered content.