How can i calculate time difference in swift 2 between two dates/time and store in 2 different variables, one showing the full difference in terms of "Year, Month, Days, Hours, Minutes" & one variable for converting the total time difference in minutes.

For example-

First Date/time - 04/13/2016 8:11 AM
Current Date/Time - 04/22/2016 12:53 AM

Now one variable should display difference as "8 Day 16 hours & 43 Minutes", if there was a year & month difference, it should show that as well.

And second variable should simply convert "8 Day 16 hours & 43 Minutes" into minutes and should show only a integer for minutes.

Please note that first date can be a future date so it shouldn't display -tive time difference.

Please help.


Recommended Answers

All 3 Replies

Convert the date+time into seconds from the "epoch". There are functions to do this in php, java, C/C++, C#, et al. Then you just subtract one from the other to determine the number of seconds between the two values. RTFM.

commented: This is fine as it may make the code more portable. +14

Thanks Rubberman, Can you suggest any function in swift 2?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.