计算当前时间和指定时间的时间差

这段代码计算出了相差几天

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

* 计算剩余时间
*
* @param endTime 结束日期
*
* @return 剩余时间
*/
-(NSString *)getCountDownStringWithEndTime:(NSString *)endTime {

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];

NSDate *now = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];//设置时区
NSInteger interval = [zone secondsFromGMTForDate: now];
NSDate *localDate = [now dateByAddingTimeInterval: interval];
endTime = [NSString stringWithFormat:@"%@ 23:59", endTime];
NSDate *endDate = [dateFormatter dateFromString:endTime];
NSInteger endInterval = [zone secondsFromGMTForDate: endDate];
NSDate *end = [endDate dateByAddingTimeInterval: endInterval];
NSUInteger voteCountTime = ([end timeIntervalSinceDate:localDate]) / 3600 / 24;

NSString *timeStr = [NSString stringWithFormat:@"%d", voteCountTime];

return timeStr;
}