1

I have a Django model with time field: time_from = models.TimeField(). I currently have a web form that sends the time in hours, for example: 12, 13, 4, 5 ... etc ...

I currently have this code: time.strptime(str(int(request.POST['time_to']) / 60), "%H") but it's throwing an error:

'time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=3, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format.

How do I go about saving, for example, 4 (that means 4AM) in my model?

4

1 に答える 1

4

時間だけが必要な場合、最も簡単な方法はまったく解析しないことです。

import datetime
myhour= '4'
mytime= datetime.time( hour= int(myhour) )
于 2012-11-20T06:24:39.713 に答える