您的位置:首页 > 其它

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

2015-12-30 14:50 323 查看
/**
*  计算剩余时间
*
*  @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;
}


这段代码计算出了相差几天
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: