如何计算iOS中两个日期之间的分钟时差 [英] How to calculate time difference in minutes between two dates in iOS

查看:416
本文介绍了如何计算iOS中两个日期之间的分钟时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何计算iOS中两个日期之间的小时数

我是Objective-C的初学者,所以我需要你的语法帮助。

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

我的问题是这样的:

我有2个字符串,我想将其转换为 NSDate 然后计算时间差以分钟为单位的日期。

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

我该怎么做?

推荐答案


  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.

这篇关于如何计算iOS中两个日期之间的分钟时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆