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,’,)

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"



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'

Leave a Reply