Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

getting TypeError: can only concatenate tuple (not "str") to tuple with creating datetime

I need to convert a date 2017-12-03 to the format 2017-12-03T00:00.000Z but am running into the error:

    TypeError: can only concatenate tuple (not "str") to tuple

I am concatenating like this:

    start_date_formatted = start_date + "T00:00.000Z"

The start_date is a tuple. If I print it out it appears in the terminal as (‘2017-12-03,’,)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried using

    from ast import literal_eval as make_tuple

but this creates another error.

What is the correct way to concatenate to format the date?

>Solution :

Very obscure way to do it, but you can change this tuple to a string, remove not needed characters and do whatever you wanted to do with it.

start_date = ('2017-12-03,',)
start_date = str(start_date)
start_date = start_date.replace('(', '').replace(')', '').replace(',', '').replace("'", '')
start_date_formatted = start_date + "T00:00.000Z"

Output

"2017-12-03T00:00.000Z"

P.S. Using re will look much better when replacing all these characters, just didn’t think about it when I wrote this answer.

Also, this is a better way for string formatting.

start_date_formatted = f'{start_date}T00:00.000Z'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading