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.

Picture 1 of Current date and time in Python

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

Update 13 July 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile