Converting Numbers to Time

converting numbers to time

Here's a sub solution using a regular expression:

set.seed(1); times <- paste0(sample(0:23,10), sample(0:59,10)) # ex. data
sub("(\\d+)(\\d{2})", "\\1:\\2", times) # put in delimitter
# [1] "6:12" "8:10" "12:39" "19:21" "4:43" "17:27" "18:38" "11:52" "10:19" "0:57"

How to convert number to time in python?

We can create a function that takes a string and returns the time.

This can all be done in one line by slicing the string up to the minutes (done with [:2]) and then concatenating a ':' and finally concatenating the minutes with [2:].

def getTime(t):
return t[:2] + ':' + t[2:]

and some tests to show it works:

>>> getTime("1230")
'12:30'
>>> getTime("0730")
'07:30'
>>> getTime("1512")
'15:12'

Note how the function cannot take an integer and convert this to a string, as otherwise entries with leading zeros would fail. E.g. 0730 wouldn't work.


Yes, to answer @AbhishtaGatya, this could be written using a lambda function, but doing so wouldn't be advisable. However:

getTime = lambda t: t[:2] + ':' + t[2:]

works just the same as above.

Convert numbers to time in R

Converting a number of seconds (such as 75) to a decimal format such as minutes.seconds is a bit odd, but you could do it this way:

a <- 75
hour <- floor(a / 60) # floor() rounds down to the last full hour
minute <- a %% 60 * 0.01 # the %% operator gives you the remainder in base 60
new_time <- hour + minute

1.15

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):

function num2time(num){  var h,m="00";  if(num<100)    h=num;  else {    h=Math.floor(num/100);    m=("0"+(num%100)).slice(-2);  }  h=h%24;  return ("0"+h).slice(-2)+":"+m;}
console.log(num2time(8));console.log(num2time(801));console.log(num2time(24));console.log(num2time(2401));console.log(num2time(2115));console.log(num2time("8"));console.log(num2time("2115"));

Converting number to time in excel - variable number of digits

You can use TIME with some math functions:

=TIME(INT(A1/1000000),MOD(INT(A1/10000),100),MOD(A1/100,100))

TIME takes 3 parameters: hours, minutes and seconds.

To get the hours, I'm dividing by 1000000, then INT rounds it down to the closest integer.

To get the minutes, I'm first dividing by 10000, but there is still the hours in that result. So I use MOD (which gives the remainder when a number is divided by another number). In the first example, the division gives 2243, and the remainder when dividing this by 100 is 43, which is the number of minutes I'm looking for.

To get the seconds, I divide the number by 100 and similar to the minutes, I use MOD to remove the minutes and hours parts. I am not using INT here in case there are milliseconds, which will be kept using this formula.

Also note that I am using the formatting hh:mm:ss.00, because excel complains if I try using hh:mm:ss:00.

Sample Image

Converting numbers into time in R

We format the numbers with sprintf to pad leading 0 for 3 digit numbers, use strptime and then use format to get the hour and min.

format(strptime(sprintf('%04d', v1), format='%H%M'), '%H:%M')
#[1] "09:30" "10:50" "12:25"

Or another option is

sub('(\\d{2})$', ':\\1', v1)
#[1] "9:30" "10:50" "12:25"

data

v1 <- c(930, 1050,1225)


Related Topics



Leave a reply



Submit