2

After I parsed a date in Python I need to patch it. But structure time.struct_time has read-only properties only:

parsed = time.strptime("23:59", "%H:%M")
parsed.tm_year = 2011
> TypeError: readonly attribute

How do I get a patched datetime value in a short & clever way?

1 Answer 1

2

Use datetime:

>>> p = datetime.datetime.strptime("23:59", "%H:%M")
>>> p = p.replace(year=2011)
>>> p
datetime.datetime(2011, 1, 1, 23, 59)
Sign up to request clarification or add additional context in comments.

Comments

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.