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

末鹿安然 提交于 2019-12-04 19:27:03

问题


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?


回答1:


  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.



来源:https://stackoverflow.com/questions/10373911/how-to-calculate-time-difference-in-minutes-between-two-dates-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!