1

I have this json response:

"dataDeNascimento":"1980-03-30T00:00:00",

What i need is to the response be like this:

30/03/1980

The part of my code how represente this json:

        response = session.get(url, headers=headers)
        resp_json2 = response.json()
        nascimento = resp_json2['dataDeNacimento']

If you guys can help me i appreciate.

2 Answers 2

2

You can use datetime from datetime module to convert your date string into datetime object then reconvert it into your desired date string again.

For example:

from datetime import datetime


date_ = "1980-03-30T00:00:00" 
new_date = datetime.strptime(date_, '%Y-%m-%dT%H:%M:%S').strftime('%d/%m/%Y')
print(new_date)

Output:

30/03/1980

Edit:

As @Mark Meyer suggested and if you're using Python >= 3.7 your can use datetime.fromisoformat

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

1 Comment

Maybe worth pointing out that you can use datetime.fromisoformat(date_) here, which might be easier on the eyes.
-1

You can chop up your string nascimento like this:

nascimento = resp_json2['dataDeNacimento'][:10]

Which will give you the first 10 characters as a string.

"1980-03-30"

1 Comment

What i need is to the response be like this: 30/03/1980

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.