0

I'm using time.strptime to parse a string as a date, but the string does not have a year associated with it. How can I add the current year to the date object?

2 Answers 2

2
import time, datetime

def currentyear(atime):
    atime = tuple([datetime.datetime.now().year] + list(atime)[1:])
    return time.localtime(time.mktime(atime))

newtime = time.strptime("19 Jan", "%d %b")
newtime = currentyear(newtime)
print newtime

http://ideone.com/vI9S0

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

Comments

1
>>> import datetime
>>> import time
>>> dt = time.strptime('12/31/' + str(datetime.datetime.now().year), '%d/%M/%Y')
>>> print dt
time.struct_time(tm_year=2012, tm_mon=1, tm_mday=12, tm_hour=0, tm_min=31, tm_sec=0, tm_wday=3, tm_yday=12, tm_isdst=-1)

This is far from the only way to do it.

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.