Current date and time in Python
How to get the current date and time in Python? There are many different ways to do this, and in this article we will apply the class date, class time in the datetime module to display the current date 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
- Strptime () function in Python
- Strftime () function in Python
- Datetime in Python
- Module time in Python
- Function sleep () in Python
- More than 100 Python exercises have solutions (sample code)
- Bookmark 5 best Python programming learning websites
- For in Python loop
- Manage files and folders in Python
- Multiple choice quiz about Python - Part 3
- 5 choose the best Python IDE for you
- What is Python? Why choose Python?
Maybe you are interested
This program will help you become invisible before the webcam lens is recording live Brain research explains the difference in the impact of technology on men and women Reveal 8 'immortal' magic tricks - the only thing you see turned out to be no magic at all! 'Eyes glued' to the screen before going to bed is not harmful to the mental health of the bar, teenager! Can ants survive if they fall from the roof of the building? How far is the bullet fired from underwater AK-47 guns?