Google News
logo
Python Program to The strftime() Method
The `strftime()` method is used to format the date and time in a specific format.

Here's an example program that demonstrates the usage of the `strftime()` method :
Program :
import datetime

# get current date and time
now = datetime.datetime.now()

# format date and time using strftime()
date_string = now.strftime("%d-%m-%Y %H:%M:%S")

# print the formatted date and time
print("Formatted date and time:", date_string)
Output :
Formatted date and time: 27-04-2023 13:04:43
In the above program, we first import the `datetime` module. We then get the current date and time using the `now()` method of the `datetime` class.

Next, we use the `strftime()` method to format the date and time in the desired format. In this case, we have used the format string `"%d-%m-%Y %H:%M:%S"`, which represents the day, month, year, hour, minute, and second in the desired format.

Finally, we print the formatted date and time using the `print()` function.