Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Current Date and Time in Python

Explore Current Date and Time in Python, including the main concepts, relevant details, and practical considerations.

Table of Contents

This updated guide examines Current Date and Time in Python and organizes the essential facts, background, and practical takeaways in clear American English.

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.

Current Date and Time in Python — contextual image 1

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 usetodayvariable 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

FAQ

What is Current Date and Time in Python about?

It provides a structured overview of the current date and time in python, explains the main context, and highlights practical takeaways for readers.

Why does this topic matter?

Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.

How should readers use this information?

Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.