Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate time difference in minutes between two dates in iOS [duplicate]

Possible Duplicate:
How to calculate time in hours between two dates in iOS

I am beginner to Objective-C, so I need your help in syntax making.

My problem goes like this:

I have 2 Strings, and I want to convert that into NSDate and then calculate the time difference between the dates in minutes.

How can I do this?

like image 614
Sagar Mohanty Avatar asked Apr 29 '12 16:04

Sagar Mohanty


People also ask

How do you find the difference between two dates and minutes?

Math. abs is used to be able to use the absolute difference (so new Date('2011/10/09 00:00') - new Date('2011/10/09 12:00') gives the same result). Dividing the result by 1000 gives you the number of seconds. Dividing that by 60 gives you the number of minutes.

How can I get the difference between two dates in IOS?

Getting difference between two dates is easy. You should know how to play between the dates. We will be using DateFormatter class for formatting the dates. Instances of DateFormatter create string representations of NSDate objects, and convert textual representations of dates and times into NSDate objects.

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

Discussion: If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.


1 Answers

  1. Use NSDateFormatter to convert the date strings into NSDate objects.
  2. Call -[NSDate timeIntervalSinceDate:] to get the difference between the two dates in seconds.
  3. Divide by 60 to get the difference in minutes.

Alternatively, you can use NSCalendar and NSDateComponents to calculate the difference in minutes for different calendars by using the components:fromDate:toDate:options: method.

like image 138
Ole Begemann Avatar answered Nov 15 '22 13:11

Ole Begemann