iOS計算兩個時間的時間差
阿新 • • 發佈:2019-02-07
/**
* 計算剩餘時間
*
* @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;
}