Can ChatGPT Code?

Can ChatGPT Code?

Tonight I gave ChatGPT a coding challenge.

I needed some Python code that takes a datetime string variable, adds 2 seconds to it, then returns the new variable as a string.

See the results below 👇

I've been learning and using Python for a few years now. I'm not passionate about it, it's just something I have to do in my 9-5.

It took me about 20 minutes from start to finish to come up with this working code.

ChatGPT?

~15 seconds to write a prompt.

The code is more succinct and Pythonic than what I originally wrote 🤯

The question now becomes…

Is this a good thing? AI gave me a better solution 20x faster than I could do it in.

I think AI is going to be a fantastic copilot for developers, authors, and creators in general. Don't trust it 100%, but use it to your advantage.

For the coders out there, you might've noticed that the ChatGPT solution wouldn't actually work.

You need to remove the Z from the original datetime string for it to be a valid isoformat string. Then I need to add it back in at the end.

This is the code I ended up with. A combination of mine and the AI’s.

from datetime import datetime, timedelta

# Set the date from a string and remove the Z so it works in isoformat
dt_str = '2022-12-20T14:52:30.300Z'
dt_str = dt_str[:-1]

# Parse the input string into a datetime object
dt = datetime.fromisoformat(dt_str)

# Add 20 seconds to the datetime object
dt += timedelta(seconds=20)

# Format the datetime object as a string
result = dt.isoformat()[:-3] + 'Z'

print(result)

AI should be your copilot, not the one in charge.

Scroll to Top