且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何计算iOS中两个日期之间的分钟时差

更新时间:2022-12-09 12:25:17


  1. 使用 NSDateFormatter 将日期字符串转换为 NSDate 对象。

  2. 致电 - [NSDate timeIntervalSinceDate:] 以秒为单位获取两个日期之间的差异。

  3. 除以60以分钟为单位获得差异。

  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.

或者,您可以使用 NSCalendar NSDateComponents 使用组件计算不同日历的分钟差异:fromDate:toDate:options :方法。

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