Excel formula - Calculate Days, Hours, Minutes and seconds between 2 timestamps - excel

I have a vba script which leaves a timestamp in cell A8 when run by the user like so:
ActiveSheet.Range("A8").Value = Format(Now(), "dd/mm/YYYY hh:mm:ss")
This displays the following in cell A8 (which is formatted as a date:
10/12/2016 9:15:10 AM
Next I want to be able to determine how many days, hours, minutes and seconds it has been since the last time the user ran the macro/vba code.
I am also capturing the current date/time by using =NOW() in cell B8.
Cell B8 is also formatted as a date and displays like so:
12/18/16 12:45
My formula should compare the timestamp in A8 with the current time and date, in cell B8.
=INT(A8-B8)&" days "&TEXT(A8-B8,"h"" hrs ""m"" mins """)
I have noted that the date formatting is not consistent here, and I don't know if this would matter?
For some reason I am not getting the desired result and I get this error with my formula:
Value!
Please can someone show me where I am going wrong?
In additions, if possible I do not want to display days, hours, minutes and seconds all at once like this:
0 days, 0 hours, 0 minutes, 0 seconds
What I actually want is if the time stamp is less than 60 seconds, to show something like:
55 seconds ago
Then something like:
20 minutes ago
then:
1 hour ago
then :
2 days ago
Thanks

First, it should be B8-A8, not the other way around, because B8 should be greater than A8. This change should make your initial formula work.
Second, your desired formula:
=IF(B8-A8>=1,INT(B8-A8)&" days ago",IF(HOUR(B8-A8)>=1,HOUR(B8-A8)&" hours ago",IF(MINUTE(B8-A8)>=1,MINUTE(B8-A8)&" minutes ago",IF(SECOND(B8-A8)>=1,SECOND(B8-A8)&" seconds ago", " just now"))))
p.s.: As per #YowE3K's comments, for this to work, your cells A8 and B8 should set to Date type, otherwise you should format them with the right format in the formulas. The first option should be easier.

Related

Subtracting seconds in Excel

I'm trying to subtract start time from end time to get duration, ie:
1:02 - 0:10 = 0:52
But what I'm getting is:
0:92
I'm using the 0\:00 format. Other suggested formats, such as [mm]:ss, are turning my data into numbers that I don't undestand, ie 1:02 becomes 146880:00
I just want to quickly enter a bunch of times, subtract one col from the other and be done with it.
Does anyone know a way to do that?
Solutions follow an explanation of the results showing in the question.
The format 0\:00 is really the format 000 with a colon character inserted between the first and second digits.
If a cell holds the value 102 and has a format of 0:\00 it will show as 1:02 in the worksheet but behind the scenes its value is still 102. So
1:02 - 0:10 = 102 - 10 = 92 = 0:92 in 0\:00 format
To understand the result with the [mm]:ss format, you need to understand how dates (and time) values are represented in Excel. There is a reasonable explanation on this webpage from Chip Pearson
First, as a date/time value 102 is equivalent to 0:00 on 11 April, 1900 as it is 102 days from Excel's day/time zero. Second, the format [mm]:ss expresses this elapsed time in minutes and seconds. So
102 days = 102*24*60 minutes = 146880 minutes
which gets displayed as 146880:00 in [mm]:ss format
There are a couple of ways you might resolve your problem.
The first involves entering data differently. A time can be entered directly into the worksheet as hours:minutes:seconds. So 1 minute and 2 seconds can be entered as 0:1:2 (or 00:01:02 or any variant such as 0:01:2 or 00:1:02). This is probably less convenient than just entering 102. By default, numbers entered in this way will display in a hh:mm:ss format but you can suppress the display of hours by changing the format to mm:ss or [mm]:ss. The latter should be used if any of your time values are 60 minutes or more since, the former will suppress the display of hours - for example, entering 0:61:2 (61 minutes and 2 seconds) displays as 01:02 with the former but as 61:02 with the latter.
Note that if you just enter 1:2 rather than 0:1:2 Excel interprets this as 1 hour, 2 minutes and 0 seconds and will display as 02:00 using format mm:ss or as 62:00 using [mm]:ss.
The second way allows you to enter the data as before using the 0\:00 format but requires the use of formulae to convert your entered value into seconds - so, for example, an entered value of 102 is intended to represent 1 minute and 2 seconds, gets correctly displayed as 1:02 but is converted behind the scenes to 62 seconds.
If A1 and B1 contain the entered values the then formula for A1 less B1 is
=(INT(A1/100)*60+A1 - 100*INT(A1/100))-(INT(B1/100)*60+B1 - 100*INT(B1/100))
This formula calculates its result as a number of seconds.
If this result is placed in cell C1 then the formula
=100*INT(C1/60)+(C1-60*INT(C1/60))
converts C1 to a result suitable for displaying with the 0:\00 format
Alternatively, the result in seconds can be converted to days by dividing by 24*60*60 = 86400 and displayed using a time format such as [mm]:ss

Excel Subtracting time

Currently trying to setup a formula that will calculate the hours/minutes between two different hours of time.
I currently use the following formula in Cell D3 and receive 12:35am as the answer:
=C3-B3
What I would want it to display is 35 minutes, which is the correct amount of time in between (6:42AM and 7:17AM).
Something like this would work:
=TEXT(C3-B3,"[m]")
or if you want "minutes" written after
=TEXT(C3-B3,"[m] ""minutes""")
Examples:
C3-B3 and formatted as [m] will work - either =TEXT(C3-B3,"[m]") or just giving the cell a custom number format of [m].
If Begin is 11:45PM and End is 12:15AM you'll only see ############# as both times are considered to be in the same day, while 12:15AM is the start of the next day.
Try =IF(C4<B4,(1+C4)-B4,C4-B4)
If C4 is less than B4 it adds 1 day to the value of End.
You have to multiply your formula by the amount of hours in a day (24) and the amount of minutes in an hour (60) in order to convert.
So your formula should be the following.
=(C3-B3)*24*60

Sum time in Excel to show a big number of days

I have cells with duration, specified as hours:minutes:seconds.
1726:48:00
410:10:00
599:20:00
The first line means 1726 hours, 48 minutes and 00 seconds
The second line means 410 hours, 10 minutes and 00 seconds
The third line means 599 hours, 10 minutes and 00 seconds
How do I sum these times in Excel, to get a breakdown in days, hours, minutes and seconds?
I'm afraid that might be impossible by changing the cell format.
If your number is in A1, you could use one of these 2 formulas:
=ROUNDDOWN(A1,0)&TEXT(A1," hh:mm:ss")
=INT(A1)&TEXT(A1," hh:mm:ss")
I also tried =TEXT(A1,"0")&TEXT(A1," hh:mm:ss"), but TEXT(A1,"0") sometimes does roundup which is not welcome.
The drawback of those formulas is that they return text. So you'll not be able to use the results in further calculations.
The requirement is to add a range of cells containing values called “duration”, which are expressed in Date and Time Serial Numbers and to display the result, currently formatted as [h]:mm:ss, in days, hours, minutes and seconds, (a kind of format [d] hh:mm:ss which is not allowed by the Excel application as already explained by ZygD). ZygD also proposed to use a formula to display the desired result as a text which comes with the restriction of further use in other calculations as well explained by ZygD:
=INT(A1)&TEXT(A1," hh:mm:ss")
In case that the total expressed as text needs to be expressed back as a Date and Time Serial Numbers I suggested the formula:
=SUM(LEFT(B1,-1+SEARCH(" ",B1)),
SUBSTITUTE(B1,LEFT(B1,SEARCH(" ",B1)),""))
Now if this does not satisfy the requirement there is another solution, that allows to add the Date and Time Serial Numbers showing the result as “days, hours, minutes and seconds” provided that it’s acceptable to show the result in two separated cells using the following formulas:
Values with “duration” are placed in the range C4:C6,
Totals Days in cell "C10",
Total Hours in cell "C11"
Totals Days =INT(SUM($C$4:$C$6))
Total Hours =MOD(SUM($C$4:$C$6),1)
If the totals are needed for further use just use the standard "SUM" formula to add up both, see cell C13
=SUM(C10,C11)
The "original" value can be worked backwards from ZygD's formula using the following formula:
=SUM(LEFT(B1,-1+SEARCH(" ",B1)),
SUBSTITUTE(B1,LEFT(B1,SEARCH(" ",B1)),""))

Excel how get number of hours in a time interval?

I have 2 columns with:
Night shift start: 19:00
Night end: 04:00
And I have some date columns with for each day..
Work started: 07:30
Worked ended: 22:00
I want to get the number of hours as a decimal that is between the night shift start and night end. I need to calculate the number of "night shift hours" for worked hours.
From comment: I do not want to get the total number of hours. I want to calculate the number of "night shift hours" and that is hours between 19:00-04:00
=IF(B1-A1 < 0, 1-(A1-B1),( B1-A1))
Assuming that cell A1 contains start, B1 contains end time.
Let me know, if it helps OR errors.
Time without date is not enough to do the subtraction considering the start can be the night before today.
Are you OK to try VBA?
EDIT: The formula is meaningful within 12 hour limit. I will see if it can be made simpler.
Given start time in B5 and end time in C5 this formula will give you the decimal number of hours that fall in the range 19:00 to 04:00
=MOD(C5-B5,1)*24-(C5<B5)*(19-4)-MEDIAN(C5*24,4,19)+MEDIAN(B5*24,4,19)
format result cell as number
just substract the two dates to get the difference in days, and multiply by 24 to get the difference in hours
=(B1-A1)*24
this is correct when both B1 and A1 contain a datetime value, but in case your cells contain just a time value, with no day value, and given that the calculation spans the night (there is a day change in between) you need to add one day to the difference:
=IF(B1<A1,1+B1-A1,B1-A1)*24

Excel 2010 - calculating time lapsed between 2 dates & times where it is over 31 days

I am trying to find out the time lapsed, Days, Hours & Mins, between 2 days and times?
Date Opened: Cell J1 08/08/2012 09:00:02
Date Resolved: Cell L1 10/10/2012 07:30:00
I have tried the Following:
=INT(L1-J1) &" days "&INT(MOD(L1-J1,1)*24)&" hours " & ROUND(MOD((L1-J1)*24,1)*60,0)&" minutes"
this works and gives the lapsed time of 62 days 22 hours 30 minutes
The problem with this is that produces this as a text result meaning that I cannot Average the results automatically
I then tried:
=L1-J1
The result of this is good as it allows me to use the results and work out averages & sums
But if the lapsed time is over a month it cannot work this out - so this example gives me the incorrect result of 02:22:29 (DD:HH:MM)
What i am looking for is a formula / function that will provide me with the time lapsed in Days/ hours/ mins that correctly shows the number of days even if more than 1 month and that i am able to use the results
thanks
It is a formatting issue. The format of DD:HH:MM is treating the result as a date (2-Mar-1900 22:29)
Unfortunately I don't think there is a built in format which will give total number of days
You can get the text you want by using formula =INT(L1-J1)&":"&TEXT(L1-J1,"HH:MM")but this will be a text value rather than a numerical value
Formatting it to [h]:mm:ss would give you the number of hours. Does that help in anyway?
It seems like this is something that can't be done in one cell, but would need to be handled in 2 cells. 1 for calculating the average (and can be hidden), and one for the display you are looking for.

Resources