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
- The date command in Windowsthe date command displays or sets the system date. if used without parameters, the date command displays the current system date setting and prompts you to enter a new date.
- How to get the current date in Excelthe following article summarizes the common ways to get the current date in excel fastest.
- How to change date and time on Windows 11by default, windows 11 automatically monitors and updates the system date and time for you thanks to a time server over an internet connection. however, there will be times when you need to manually adjust the time, or set up time synchronization
- How to change time, date and month in Windows 10?many users are still not familiar with the operation on windows 10, such as changing the system date and time on win 10. below, tipsmake.com will guide you to change the date and time on windows 10 to the time you want. like fixing windows 10 error showing wrong date and time.
- How to change date and time manually in macOSeven after setting up macos, changing the date and time is a simple task. in fact, if you've selected the right time zone, macos will automatically display the exact date and time for your location.
- Date functions in Excel, DAY, WEEKDAY, MONTHdo you want to automatically update the current date, separate years or calculate the distance between two time points? the article below, free download will guide you how to do this professionally with date functions in excel.
- Function sleep () in Pythonmodule time in python provides some very useful functions to handle time-related tasks. one of the most commonly used functions is sleep ().
- Macromedia Flash - Digital clocktime is money and we must always know how to hold it. this article will show you how to create a digital clock effect with the current date using flash 8.
- How to insert current time into Google Sheetsinstead of manually entering the current time into google sheets, you can use the now and today functions to do the job. these functions will insert and display the current date and time, and automatically update the value when the google sheets spreadsheet changes.
- More than 100 Python exercises have solutions (sample code)more than 100 python code examples are shared by guy zhiwehu on github, however, the solution of this series is written on the old python version. following tipsmake.com will be vietnameseized and edited to suit python 3.x to help you learn and practice python.