How to convert a datetime object to only a string of date in Python

Hello programmers, in this tutorial we will see how to convert a DateTime object to only a string of data in Python.

Often programmers need the date and time from the program. For example, when software is being executed, the developer maintains a log file that contains the modules selected with the date and time of selection of it. This helps in maintaining the security of the software system and also helps in fixing bugs for better performance and efficiency which makes the software more reliable.

Installation

Install the DateTime library using the following command in the command prompt.

pip install datetime

Generate the current date and time

We can generate the current date and time by using the inbuilt method datetime.now() available in the DateTime module of Python.

Given below is the approach how to get the current date and time.

#Import library
import datetime

#get current date and time
def getCurrDateTime():
    currDateTime = datetime.datetime.now()
    return currDateTime

#driver
if __name__ == "__main__":
    print(f"The current date and time is: {getCurrDateTime()}.")
    print(f"The type returned is: {type(getCurrDateTime())}.")

Output

The current date and time is: 2022-09-17 15:11:30.161723.

The type returned is: <class 'datetime.datetime'>.

Explanation
We import the DateTime module in Python and use the method ‘datetime.datetime.now()’ to get the current date and time of the day. In the output, we check the current date and time and also the type which is returned by the function.

Approach for converting it from datetime object to string

We can invoke the ‘str()’ method to convert the date and time to string. Given below is the illustration for the following.

#Import library
import datetime

#get current date and time
def getCurrDateTime():
    currDateTime = datetime.datetime.now()
    return currDateTime

#driver
if __name__ == "__main__":
    currDateTime = str(getCurrDateTime())
    print(f"The current date and time is: {currDateTime}.")
    print(f"The type returned is: {type(currDateTime)}.")

Output

The current date and time is: 2022-09-17 15:15:26.410296.

The type returned is: <class 'str'>.

Often programmers can format the output that the DateTime object returned from the module. We can have only the current date and convert it to string and use it for further manipulation. Given below is the illustration for the following.

#Import library
import datetime

#get current date
def getCurrDate():
    currDate = datetime.datetime.now()
    return currDate.strftime('%d/%m/%Y')

#driver
if __name__ == "__main__":
    currDate = str(getCurrDate())
    print(f"The current date and time is: {currDate}.")
    print(f"The type returned is: {type(currDate)}.")

Output

The current date and time is: 17/09/2022.

The type returned is: <class 'str'>.

Explanation

We get the current date and time from the DateTime module which is a DateTime object returned. We then use the ‘strftime()’ method which is the string format time in which we arrange the date as ‘day/month/year’ and convert it into a string and print the value.

Also read: Get the current date and time in every possible format in Python

Leave a Reply

Your email address will not be published.