Is timestamp difference always INTERVAL DAY TO SECOND? - apache-spark

My observations show that timestamp difference has type of INTERVAL DAY TO SECONDS:
select typeof(getdate() - current_date())
-----------------------------------------
interval day to second
But is it guaranteed? Can it be DAY TO MINUTE or YEAR TO MONTH depending on the input? I could not find that in the documentation.
This becomes important when converting an interval to numeric format, since CAST() returns the number of lower units in the interval.
P.S. I am aware of other ways to convert time delta into seconds.

It is documented:
If expr1 and expr2 are DATEs the result is an INTERVAL DAYS.
If expr1 or expr2 are TIMESTAMP the result is an INTERVAL DAY TO SECOND.
It can be INTERVAL YEAR TO MONTH only when subtracting two year-month intervals.
And I think you can't cast an interval to a number...

Related

Spotfire: Timespan/Date Converted to Real Number?

Is there a possibility that a Timespan/date can be converted to a real number?
I'm trying to calculate the average days it takes to complete a job, per job category. But I believe the bar chart is having issues with calculations because of the timespan format.
Thanks!
If you have a TimeSpan data type, you can turn it into a real number by wrapping the Real(..) function around it. If you have a date, you could for instance use the function ToEpochSeconds(..) to turn it into the number of seconds from start of 1970. Then you can divide it by 60 * 60 * 24 to turn it into days.
There are other functions available if you look into Conversion functions or Date and time functions.

How to conditionaly check an hour difference between two dates in excel

I have a date with hours and minutes like 2/3/2019 10:30 am , and I do the following to find the diffence in hours which I already formatted like ( h )
=now() - date
I want to check if the difference is more than 12 ( where I mean 12 hours) to do something.
if(actual difference>12; "hello"; "bye")
But is not checking the actual difference but the serial difference
How can I check the actual difference in hours?
Dates are stored as numbers where the integer part is the number of days since 1899-12-31. The fractional part measures the time within a day. So, 24 * (now() - date) will give you the number of hours elapsed.
NB: the formatting is only a question of display and won't influence what's done by the formulas.
In order to check conditionaly the difference in the hours unit between two times we can do if(hour(actual difference)>12; "hello"; "bye")
We put the function hour() as noted in
https://support.office.com/en-us/article/calculate-the-difference-between-two-times-e1c78778-749b-49a3-b13e-737715505ff6

Calculate time difference between two dates (DD/MM/YYY HH/MM)

In Excel, I have these kind of fields (in the second and the third column):
Fecha/Hora inicio Fecha/Hora finalizaciĆ³n
02/01/2017 21:32 03/01/2017 6:02
02/01/2017 6:02 03/01/2017 7:32
03/01/2017 7:38 04/01/2017 13:47
I want to know the time difference between the first and the second field in hours. For instance, in the second case, the difference is 25.5 hours.
I was trying to calculate it manually but I'm sure there is an automatic way.
VBA has a function called DateDiff which measures the difference between two dates in terms of a specified unit (anything from years to seconds). For this case, we will need to measure in minutes because we want to measure fractions of an hour and then divide by 60 to get back to hours. If we only wanted whole hours then we could measure in hours instead.
The following should provide the required result:
MsgBox DateDiff("n", "02/01/2017 6:02", "03/01/2017 7:32") / 60
"n" is the interval specifier for minutes in the DateDiff function

How to round time to nearest hour in Excel?

How to round time to nearest hour in Excel, for example:
67:45:00 will be 68:00:00
and
53:14:00 will be 53:00:00
regards
You can use MROUND function like this
=MROUND(A1,"1:00")
Assuming the time is in A1 this will round to closest hour.
=ROUND(A1*24,0)/24
Using ROUNDDOWN will force 67:45:00 to 67:00:00
Using ROUNDUP Will force 67:45:00 to 68:00:00
Same formula, change:
24 to 48 for half hours.
24 to 96 for quarters
If you are grouping hourly in a 24 hour span but the date is included in the time stamp, use the same formula but subtract the datevalue after:
=ROUNDDOWN(A1*24;0)/24-INT(A1)
This is useful if you want to see at what time of day something peaks over a period of time.
Transform it to hours (5h 15m = 5.25h) then round it
if you only have it as a string use
=if(round(mid(A1;4;2);0)>29;mid(A1;1;2)+1&":00:00";mid(A1;1;2)&":00:00")
i use round to convert the minutes into a number
I recently had to convert times to the nearest quarter hour. I used the following sequence of formulas, which seemed to work:
=SUM(A1*24*60) - this converts the time to minutes
=MOD(B1,15) - this finds the minutes since the last quarter hour
=IF(C1>7,15-C1,-C1) - minutes needed to round up or down to nearest quarter hour
=SUM(D1/(24*60)) - converts the adjustment needed from minutes back to a days
=SUM(A1+E1) - this is the original time adjusted up or down to the nearest quarter hour

How to get the difference in minutes between two dates in Microsoft Excel?

I am doing some work in Excel and am running into a bit of a problem. The instruments I am working with save the date and the time of the measurements and I can read this data into Excel with the following format:
A B
1 Date: Time:
2 12/11/12 2:36:25
3 12/12/12 1:46:14
What I am looking to do is find the difference in the two date/time stamps in mins so that I can create a decay curve from the data. So In Excel, I am looking to Make this (if the number of mins in this example is wrong I just calculated it by hand quickly):
A B C
1 Date: Time: Time Elapsed (Minutes)
2 12/11/12 2:36:25 -
3 12/12/12 1:46:14 1436.82
I Have looked around for a bit and found several methods for the difference in time but they always assume that the dates are the same. I exaggerated the time between my measurements some but that roll over of days is what is causing me grief. Any suggestions or hints as to how to go about this would be great. Even If I could find the difference between the date and times in hrs or days in a decimal format, I could just multiple by a constant to get my answer. Please note, I do have experience with programming and Excel but please explain in details. I sometimes get lost in steps.
time and date are both stored as numerical, decimal values (floating point actually). Dates are the whole numbers and time is the decimal part (1/24 = 1 hour, 1/24*1/60 is one minute etc...)
Date-time difference is calculated as:
date2-date1
time2-time1
which will give you the answer in days, now multiply by 24 (hours in day) and then by 60 (minutes in hour) and you are there:
time elapsed = ((date2-date1) + (time2-time1)) * 24 * 60
or
C3 = ((A3-A2)+(B3-B2))*24*60
To add a bit more perspective, Excel stores date and times as serial numbers.
Here is a Reference material to read up.
I would suggest you to use the following:
Combine date to it's time and then do the difference. So it will not cause you any issues of next day or anything.
Please refer to the image with calculations. You may leave your total minutes cell as general or number format.
MS EXCEL Article: Calculate the difference between two times
Example as per this article
Neat way to do this is:
=MOD(end-start,1)*24
where start and end are formatted as "09:00" and "17:00"
Midnight shift
If start and end time are on the same day the MOD function does not affect anything. If the end time crosses midnight, and the end is earlier then start (say you start 23PM and finish 1AM, so result is 2 hours), the MOD function flips the sign of the difference.
Note that this formula calculates the difference between two times (actually two dates) as decimal value. If you want to see the result as time, display the result as time (ctrl+shift+2).
https://exceljet.net/formula/time-difference-in-hours-as-decimal-value
get n day between two dates, by using days360 function =days360(dateA,dateB)
find minute with this formula using timeA as reference =(timeB-$timeA+n*"24:00")*1440
voila you get minutes between two time and dates
I think =TEXT(<cellA> - <cellB>; "[h]:mm:ss") is a more concise answer. This way, you can have your column as a datetime.

Resources