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?
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.
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.
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.
NSDateFormatter
to convert the date strings into NSDate
objects.-[NSDate timeIntervalSinceDate:]
to get the difference between the two dates in seconds.Alternatively, you can use NSCalendar
and NSDateComponents
to calculate the difference in minutes for different calendars by using the components:fromDate:toDate:options:
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With