Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the difference in time between two NSDates Swift [duplicate]

Tags:

swift

nsdate

I have two NSDate objects: startDate and endDate, both are NSDate objects. I want to find the difference between these two dates. I know there is a resolved question about this already (Getting the difference between two NSDates in (months/days/hours/minutes/seconds)), but I want to know if there is a quicker way to do this (less execution code). Is that possible?

like image 752
ButtonMasterBot Avatar asked Jul 24 '15 16:07

ButtonMasterBot


People also ask

How do I get the time difference between two dates in Swift?

To calculate the time difference between two Date objects in seconds in Swift, use the Date. timeIntervalSinceReferenceDate method.

How do you find the difference in time between two timestamps?

The difference is calculated by subtracting the second operand from the first. The result is rounded down, with any remainder discarded. For example, 61 minutes is equal to 1 hour, and 59 minutes is equal to 0 hours.

How do I find the difference between two dates in the same column?

You can use LAG function to get the desired result you want. Basically, LAG function access the data of the previous row. From there, you can now calculate the difference between the previous date and the current date using DATEDIFF function.


1 Answers

Yes if you look at the second answer to your linked question you can do this: let interval = laterDate.timeIntervalSinceDate(earlierDate)

like image 177
pbush25 Avatar answered Sep 28 '22 07:09

pbush25