0

I'm trying to convert a string to datetime with dd/mm/YYYY format, but the function strptime seems isn't working properly. The result format isn't in the same that i am specifying in the function.

import datetime
from datetime import datetime
from datetime import date

today = date.today()
print (today)
today = today.strftime("%d-%m-%Y %H:%M:%S")
print (today)

today = datetime.strptime(today, "%d-%m-%Y %H:%M:%S")
print (today)

My output is this:

2022-08-26
26-08-2022 00:00:00
2022-08-26 00:00:00

it seems that after the strftime function, the format is right, but when I call the strptime function, it's getting back to the previous format "YY-mm-dd"

3
  • after strptime, today is a datetime object - which has not intrinsic formatting. If you need formatting, use strftime again Commented Aug 26, 2022 at 18:49
  • You should probably read the documentation and note the object type returned by strptime Commented Aug 26, 2022 at 18:50
  • The question says that the string format is dd/mm/YYYY. But %d-%m-%Y is for dd-mm-YYYY format (hyphens rather than slashes). Which is correct? Commented Mar 7 at 23:14

3 Answers 3

3
today = datetime.strptime(today, "%d-%m-%Y %H:%M:%S")
print (today)

That part is creating a datetime object. You can do:

print (today.strftime("%d-%m-%Y %H:%M:%S"))

again in the final line

Sign up to request clarification or add additional context in comments.

Comments

1

it seems that after the strftime function, the format is right, but when i call the strptime function, its getting back to the previous format "YY-mm-dd"

That is because print(today) determines the format since you didn't specify one. As other's pointed out, today is a datetime object. When you print() a datetime object, python will choose a format for you, I assume based on your system's locale settings.

One concept to understand here is the difference between a value and the representation of that value as a string. This is what formatting a datetime is for.

I suggest you don't worry about the output here too much. As long as your explicit format with strftime() gives you the correct representation, then you are good. This formatting should only be used for output. Otherwise, store datetimes and datetime objects in your variables.

Comments

-1
from dateutil.parser import parse
date_obj1='08-02-25'
date_obj1 = parse(date_obj1)
date_obj2=date_obj1.date()
# below will in String form which is not operatable data processing
print(type(date_obj1)
# below is in date format which in operatable in data processing
print(type(date_obj2))

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.