[英]Objective-C midpoint between two times
我有两个NSDate格式为“ h:mm a”作为时间(即6:00 AM和8:00 PM)。
我试图找出那两次之间的中点是什么时间。
对于上面的示例,介于6:00 AM和8:00 PM之间的中点将为1:00 PM。
但是我不知道如何在Objective-C中为iPhone SDK执行此操作。
任何帮助或建议或代码将不胜感激。
您追求的可能是以下内容?
// Assuming you have two NSDate instances: start and end.
NSTimeInterval difference = [end timeIntervalSinceDate:start];
NSDate *middle = [NSDate dateWithTimeInterval:difference / 2 sinceDate:start];
有关更多信息,请参见NSDate类参考 。
将时间转换为时间间隔,将两个数字平均,然后将其转换回NSDate对象。
假设您有两次是NSDate实例,则可能如下所示:
NSTimeInterval intA = [timeA timeIntervalSince1970];
NSTimeInterval intB = [timeB timeIntervalSince1970];
NSDate *avgTime = [NSDate dateWithTimeIntervalSince1970:(intA + intB) / 2.0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.