Total minutes from datetime value - excel

How to get total time in minutes from datetime?
18/11/2013 07:31:04
The result should be: 451 min (it's 07*60 + 31).

Extract the hour and minute components of your date time with HOUR() and MINUTE() functions and combine them with this formula
=HOUR(A1)*60+MINUTE(A1)

Related

How to convert timestamp into milliseconds in python

I am trying to write a basic script that can read in a timestamp as a string and convert it into milliseconds. The timestamps I am working with are in minute:second.millisecond format.
from datetime import datetime
timestamp_start = '54:12.123'
MSM = '%M:%S.%f'
zero = '00:00.000'
start_sec = (datetime.strptime(timestamp_start, MSM) - datetime.strptime(zero, MSM)).total_seconds()
start_ms = start_sec * 1000
print(start_ms)
This may be a round about approach, but I am first using datetime.strptime to get a datetime object, then subtracting by 0 in order to get a timedelta object, getting the total seconds of the timedelta object, and finally multiplying by 1000 to convert to milliseconds.
The above code works fine, except for any timestamps over an hour.
The issue that I am running into- the timestamps do not have an hour counter. For example: 1 hour, 5 minutes, and 30 seconds comes in as 65:30.000. datetime.strptime cannot recognize this format, as it only allows the minutes to be between 0 and 59.
How can I convert these timestamps into a format recognizable by datetime? Should I first get the timestamp into hour:minute:second:millisecond format? Keep in mind the end goal is to convert these timestamps into milliseconds. If there is a better approach any suggestions are more than welcomed!
'54:12.123' isn't really a timestamp, but elapsed time, and there's no built-in method in Python that can deal with elapsed time with a format string like a timestamp format.
Since the format string in question is simply minutes and seconds separated by a colon, and seconds and milliseconds separated by a period, you can easily parse it with the str.split method:
def convert(msf):
minutes, seconds = msf.split(':')
seconds, milliseconds = seconds.split('.')
minutes, seconds, milliseconds = map(int, (minutes, seconds, milliseconds))
return (minutes * 60 + seconds) * 1000 + milliseconds
so that convert('54:12.123') returns:
3252123

Handle different time formats in a dataframe

I am working on a dataframe with a column regrouping different time format like
Time ID ...
0 1 hrs 1 min 1 sec 1
1 1 min 1 sec 2
2 1 sec 1
I would like to calculate the mean of the time column grouped by ids.
My problem is that the time format depends of the row.
I tried to use the mean() function on the Time column
df[["ID", "Time"]].groupby(["ID"]).agg(lambda x: x.mean())
but it does not work.
I tried to format to date to then calculate the mean, but the
format="%H hrs %M min %S sec" only apply to the first case and I get an Error:
ValueError: time data '1 min 1 sec' does not match format '%H hrs %M min %S sec' (search)
Convert Time to Timedelta and convert to seconds and call mean. Before doing it, you need replace hrs to hours.
s = pd.to_timedelta(df.Time.replace('hrs', 'hours', regex=True)).dt.total_seconds()
s.groupby(df.ID).mean()
Out[110]:
ID
1 1831.0
2 61.0
Name: Time, dtype: float64

Spotfire AVG value per hour

I have users that have random values per hour across 24 hours. I want to get their average value per hour as it increases. Such as: a value of 3 at 3pm, then 4 at 4pm, 5 at 5pm, find the average per hour and give the total average once there are no more timestamps.
I've tried this:
case
when (DatePart("hour",[AUDIT_TSP])>0) and (DatePart("hour",[AUDIT_TSP])<1)
then Date([AUDIT_TSP]) & " " & ":00" & ":00" & Second([AUDIT_TSP])
when (DatePart("hour",[AUDIT_TSP])=1) and (DatePart("minute",
[AUDIT_TSP])>0) then Date([AUDIT_TSP]) & " " & ":01" & ":00" &
Second([AUDIT_TSP])
else null end
This was based off of sporfire: calculate the avg per 15 minutes and I tweaked it for my use but couldn't get the code to show the avg hour and not 15min avg. So I figured to ask here.
My AUDIT_TSP is formatted with DateTime and example values look like:
4/15/2019 6:16:59 AM
4/15/2019 6:20:05 AM
The values are just shipments so, 1 shipment an hour, 2 shipments an hour, etc. Just trying to get the average per hour.
I don't expect the average per hour to show up on the timeline, the values it's showing here is the amount of shipments for each hour. If the average can be shown on the timeline, then great, if not, then I can audible and show it in a textbox if that's possible as well.
You could produce an intermediate column to mark each hour with
[hourBin] calculated as: Integer(ToEpochSeconds([time-stamp]) / 3600)
Here [time-stamp] is your timestamp column. ToEpochSeconds(..) counts the number of seconds from a reference date (usually 1st Jan 1970). When you divide it by 3600 and take the integer part, you get an hour counter.
Then average your values for each hour
Avg([value]) OVER ([hourBin])
And/or visualise the average and the spread around it in a box-plot of [value] with [hourBin] as the category.
If you want the intermediate column to look more meaningful you can rescale it by its first value so it starts from 0 (or add any number you wish):
Integer(ToEpochSeconds([time-stamp]) / 3600) - Min(Integer(ToEpochSeconds([time-stamp]) / 3600))

Powerpivot Dax- Calculate number of Hours after a fixed time

I have a list of times in a column [Logtime]:
11:45:44PM
07:05:05PM
I'd like to create a measure that returns the total number of hours after 6:30PM. So given the above times:
5.5
0.58
HoursAfter:=[logtime] -6:30PM doesn't work.
Hour[logtime] - hour(18.5) also doesn't work
EDIT:
timevalue([logtime]) - timevalue("05:00:00") works but it returns a datetime ala
12/30/1899 5:17:16PM
I need to convert the time 5:17:16 into decimal hours i.e. 5.26, how can I do this?
There may be a slightly more elegant way, but I was able to create a calculated column that does what you want as follows,
HoursAfter = DATEDIFF(TIMEVALUE("6:30 PM"), Times[LogTime], SECOND) / 3600
This takes the time difference between 6:30 PM and your LogTime in units of seconds and then converts it to hours by dividing by 60*60 = 3600.
Edit: A simpler formula can be written as follows,
HoursAfter = 24 * (Times[LogTime] - TIMEVALUE("6:30 PM"))
(Multiply by 24 since the datetime values are stored in units of days.)
timevalue([logtime]) - timevalue("05:00:00")

Excel Function For If Then Statement for Range of Times

=IF(I44<"0:01","0",IF(I44<"0:30","2:00",IF(I44<"1:00","2:30",IF(I44<"1:30","3:00",IF(I44<"2:00","3:30",IF(I44<"2:30","4:00",IF(I44<"3:00","4:30",IF(I44<"3:30","5:00",IF(I44<"4:00","5:30",IF(I44<"4:30","6:00",IF(I44<"5:00","6:30",IF(I44<"5:30","7:00",IF(I44<"6:00","7:30",IF(I44<"6:30","8:00",IF(I44<"7:00","8:30",IF(I44<"7:30","9:00",IF(I44<"8:00","9:30",IF(I44<"8:30","10:00",IF(I44<"9:00","10:30",IF(I44<"9:30","11:00",IF(I44<"10:00","11:30","")))))))))))))))))))))
(Left out the code tags so you dont have to scroll 30 pages to the right)
I am using this function to add specific amounts of time to specific range of time and this seems like there must be a better method of doing this.
For example: for a input time of < 30 min, would output 2:00 hr, for input time of < 1:00 would output 2:30 ... and for each 30 min increment in the input the output would increment by 30 minutes
Perhaps just round down to the next half hour then add 2 hours, i.e
=IF(I44=0,0,FLOOR(I44,"0:30")+"2:00")
[with an IF to deal with zero values]
Format result cell as a time value, e.g. h:mm or similar

Resources