How to Convert an Integer to Time

Convert an Integer to time

You could do something like this.

select cast(DATEADD(hour, 13, 0) as time)

The upside is that it will still work even with negative numbers or values over 24.

How do I convert integer into a time format

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

PHP convert integer to time

so something like:

$time='7.5'; //, 8.3, 2.2. 

$x=explode('.',$time);

$min=60*($x[1]/10);

echo $x[0] .'hours and '.$min.'mins';

pandas convert integer to time

Use Series.str.zfill with convert integers to strings:

time = pd.to_datetime(temp['time'].astype(str).str.zfill(6), format='%H%M%S').dt.time
print (time)
0 00:01:00
1 10:00:00
2 23:59:00
Name: time, dtype: object

If want timedeltas:

s = temp['time'].astype(str).str.zfill(6)

td = pd.to_timedelta(s.str[:2] + ':' + s.str[2:4] + ':' + s.str[4:])
print (td)
0 00:01:00
1 10:00:00
2 23:59:00
Name: time, dtype: timedelta64[ns]

Or:

td= pd.to_timedelta(time.astype(str))
print (td)
0 00:01:00
1 10:00:00
2 23:59:00
Name: time, dtype: timedelta64[ns]

Is there anyway to convert those int into time?

You can also outsource all the division and modulo operations to Python built-ins (remember: batteries are included!)

>>> import time
>>> x = 40000
>>> time.strftime('%H:%M:%S', time.gmtime(x))
'11:06:40' # <- that's your desired output
>>> time.gmtime(x)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=6, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)
>>> time.gmtime(x).tm_hour # <- that's how to access individual values
11

How to convert int number into time - SQL

Use CONVERT() -- (http://dev.mysql.com/doc/refman/5.7/en/charset-convert.html)

You'll have to pad it with zeros to show CONVERT() that it is in hours and not minutes.

Specific to your example:

> SELECT CONVERT(CONCAT('1000','00'), TIME) AS time1;
time1
10:00:00

or

> SELECT CONVERT(CONCAT(`fieldname`,'00'), TIME) AS time1 FROM `tablename`;
time1
10:00:00

How to convert an integer to time

Here's the solution by using pandas and datetime python library. I hope it will help you

Code:

import pandas as pd
from datetime import datetime
hour = 13
a = pd.to_datetime(hour, format='%H')
converted_time = a.strftime("%H:%M:%S")
print(converted_time)

Output

13:00:00

How to convert a number to time?

Conversion based on <100 (hours-only) and >=100 (100*hours+minutes), plus some fight with 24 and single-digit numbers (both hours and minutes):