0
votes
time  = datetime.strptime(str(shift_change_ob.shift_date) ,"%Y-%m-%d %H:%M:%S").strftime("%H:%M")
self.orginal_shift_time = float(time)

type(time) is string while type(self.orginal_shift_time) is float. So I am getting ValueError: invalid literal for float(): 07:00. How can I assign string value to float field?

2
07:00 should be converted to what float? - mohammad
"print time" prints 07:00. But type(time) is string. I want to assign it to a field whose type is float. - Aiswarya
07:00 is not assignable to float. Floats are of the form "int.int" - mohammad
you want 07:30 in float like 7.5 right? which means 7 an half hour! - DexJ

2 Answers

1
votes

Sine you were using colon strftime("%H:%M") to seperate hours and minutes, it is not possible to convert to float.

So replace colon with dot strftime("%H.%M"), now it will works.

>>> time = strftime("%H.%M")
>>> shift_time = float(time)
>>> shift_time
10.55
0
votes

The time variable will have string, If you want hour and minute.. use datetime.hour and datetime.minute. I am assuming shift_change_ob.shift_date is a datetime object. you can get the value from this object itself.

shift_change_ob.shift_date.hour
shift_change_ob.shift_date.minute 

should give what you want.

If you need to store the value as a single float variable you can,

time  = datetime.strptime(str(shift_change_ob.shift_date) ,"%Y-%m-%d %H:%M:%S").strftime("%H:%M")
f_time = float(time.split(':')[0] + '.' + str(float(time.strip(':')[1])*(10/6)))

10/6 is the factor to convert minutes into decimal.