How to get current date and time in python?

How to get current date and time in python?

In this tutorial, you will learn how to get current date and time in Python.

To get the current date and time in Python, you can use any module such as datetime, time, calendar, or even arrow module.

Let’s see how you can get current date and time in Python easily using these modules.

Method 1

How to get current date and time in Python using datetime module?

You can use the built-in datetime module to get the current date and time.

Let’s see one by one how you can get the current date and time.

To get the current date, you can use the date() method of the datetime module.

import datetime

today_date = datetime.date.today()
print(today_date)

Output

2023-04-20

The default format of current date is YYYY-MM-DD.

But, You can change this format to any desired format.

Let’s see how you can change the format of current time in python.

Change Format

Current date in different formats

To change the format of current, you can use strftime() method.

The strftime() method is a method of the date object in Python’s datetime module.

It stands for “string format time”.

You can use it to convert a date object to a string in a specified format.

The strftime() method takes one argument, which is a format string that specifies the format of the resulting string.

The format string consists of a series of codes that are replaced with the corresponding values from the date object.

Here are some common format codes:

  • %Y – Year (e.g. 2023)
  • %m – Month (e.g. 01-12)
  • %d – Day of the month (e.g. 01-31)
  • %H – Hour (e.g. 00-23)
  • %M – Minute (e.g. 00-59)
  • %S – Second (e.g. 00-59)

Here is an example of how to use the strftime() method to format current date.

import datetime

today_date = datetime.date.today()
print("Today date: ", today_date)

formatted_date = today_date.strftime("%d-%m-%Y")
print("Formatted date: ",formatted_date)

Output

Today date: 2023-04-20
Formatted date: 20-04-2023

You can use the strftime() method to format time as well.

Let’s see how you can get the current time in python and format it as per your requirement.

Date and Time

Get current date and time in Python

You can use the datetime object of datetime module to get current date and time.

The datetime module has a method now() which returns you current date and time.

Let’s see how it works.

import datetime

now = datetime.datetime.now()
print("now =", now)

Output

now = 2023-04-20 12:33:50.348894

You can see, we have got the current as well as time.

But this format does not seems good.

Let’s try to format it strftime() method that we have discussed earlier.

Here is how it works:

import datetime

now = datetime.datetime.now()
print("now =", now)

formatted = now.strftime("%d-%m-%Y %H-%M-%S")
print("Formatted =",formatted)

Output

now = 2023-04-20 12:39:50.026161
Formatted = 20-04-2023 12-39-50

You can use custom text and even newline characters in the strftime() method.

Let’s see an example of that:

import datetime

now = datetime.datetime.now()
print("now =", now)

formatted = now.strftime("Current Date: %d-%m-%Y\nCurrent Time: %H-%M-%S")
print(formatted)

Output

now = 2023-04-20 12:43:27.940030
Current Date: 20-04-2023
Current Time: 12-43-27

That’s how easy it is.

You can format any way you want.

The possiblities are limitless.

Here we used datetime.now() to get the current date and time.

Then we used the strftime() to format the string as we want.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x