MrWednesday
MrWednesday

Reputation: 27

How do I convert integer into a time format

How would I go about converting a float like 3.65 into 4 mins 5 seconds.

I have tried using:

print(datetime.datetime.strptime('3.35','%M%-S'))

However, I get this back:

ValueError: '-' is a bad directive in format '%-M:%-S'

Upvotes: 0

Views: 2361

Answers (4)

Alain T.
Alain T.

Reputation: 42133

First, you should complain to whoever is giving you time data expressed like that.

If you need to process minutes and seconds as a standalone value, then the datetime object may not your best choice either.

If you still need to convert "3.65" into a datetime object corresponding to "4-05" you could adjust it to be a valid time representation before passing it to strptime()

m,s = map(int,"3.65".split("."))
m,s = (m+s)//60,s%60
dt  = datetime.datetime.strptime(f"{m}-{s}","%M%-S")

Upvotes: 1

Edgardo Obregón
Edgardo Obregón

Reputation: 447

Take a look at the following script, you can figure out how to make it work for days years, etc, this only works if we assume the format is "hours.minutes"

import datetime

# Assuming the 3 represents the hours and the 0.65 the minutes
number = 3.65

# First, we need to split the numbero into its whole decimal part
# and its decimal part

whole_decimal_part = hours = int(number)  # 3
decimal_part = number % whole_decimal_part  # 0.6499999

# Now, we need to know how many extra hours are in the decimal part
extra_hours = round((decimal_part * 100) / 60)  # 1
minutes = round((decimal_part * 100) % 60)  # 5

hours += extra_hours  # 4

time_str = "%(hours)s:%(minutes)s" % {
    "hours": hours,
    "minutes": minutes
}  # 4:5

final_time = datetime.datetime.strptime(time_str, "%H:%M").time()

print(final_time)  # 04:05:00

Upvotes: 1

mad_
mad_

Reputation: 8273

while 65 seconds cannot be parsed correctly so you have to manipulate by yourself to clean the data first before parsing.

NOTE: assuming seconds is not a very very big number which can make minutes>60

import datetime
time= '3.65'
split_time = time.split(".")
minute =int(split_time[0])
seconds = int(split_time[1])
minute_offset, seconds = divmod(seconds, 60); 
minute = int(split_time[0]) + minute_offset
print(datetime.datetime.strptime('{}.{}'.format(minute,seconds),'%M.%S')) #1900-01-01 00:04:05

You can alternatively use .time() on datetime object to extract the time

print(datetime.datetime.strptime('{}.{}'.format(minute,seconds),'%M.%S').time()) #00:04:05

A much cleaner and safer solution is (to consider hour as well). Convert everything into seconds and then convert back to hours, minutes, seconds

def convert(seconds): 
    min, sec = divmod(seconds, 60) 
    hour, min = divmod(min, 60) 
    return "%d:%02d:%02d" % (hour, min, sec) 

time='59.65'
split_time = time.split(".")
minute =int(split_time[0])
seconds = int(split_time[1])

new_seconds = minute*60 +65
datetime.datetime.strptime(convert(new_seconds),'%H:%M:%S').time()

Upvotes: 0

jose_bacoy
jose_bacoy

Reputation: 12704

Split your time into minute and seconds If seconds is 60 or more, then add extra minutes (//) ; second is the modulo (%)

t="3.65"
m, s = [int(i) for i in t.split('.')]
if s >= 60:
    m += s//60
    s  = s % 60
print(f'{m} mins {s} seconds')  # -> 4 mins 5 seconds

Upvotes: 0

Related Questions