Python - Datetime's Time is always zero
Strange little problem I'm facing with Datetime. Here's what I'm doing:
>>> from datetime import datetime, date
>>> t = date.timetuple(datetime.now())
开发者_Go百科>>> t
time.struct_time(tm_year=2011, tm_mon=6, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=165, tm_isdst=-1)
tm_hour, tm_min and tm_sec are all zero. Why is this?
Well t
is a date
, so of course it doesn't contain any time data. You have to use datetime.timetuple(datetime.now())
to have those fields populated.
I have tried this in my console and get the following results:
from datetime import datetime, date
date.timetuple(datetime.now())
>>> time.struct_time(tm_year=2011, tm_mon=6, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=165, tm_isdst=-1)
datetime.timetuple(datetime.now())
>>> time.struct_time(tm_year=2011, tm_mon=6, tm_mday=14, tm_hour=13, tm_min=23, tm_sec=34, tm_wday=1, tm_yday=165, tm_isdst=-1)
>>> from datetime import datetime
>>> datetime.timetuple(datetime.now())
time.struct_time(tm_year=2011, tm_mon=6, tm_mday=14, tm_hour=18, tm_min=25, tm_sec=20, tm_wday=1, tm_yday=165, tm_isdst=-1)
>>> from datetime import date
>>> date.timetuple(datetime.now())
time.struct_time(tm_year=2011, tm_mon=6, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=165, tm_isdst=-1)
this is my result.
this should work:
t = datetime.timetuple(datetime.now())
精彩评论