iOS 电商购物车倒计时时间计算

/**
 * 倒计时
 *
 * @param endTime 截止的时间戳
 *
 * @return 返回的剩余时间
 */
- (NSString*)remainingTimeMethodAction:(long long)endTime
{
    //得到当前时间
    NSDate *nowData = [NSDate date];
    
    //把时间戳转换成date格式
    NSDate *endData=[NSDate dateWithTimeIntervalSince1970:endTime];
    
    //创建日历对象
    NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSCalendarIdentifierGregorian ];
    
    //设置单元标识 小时 分钟 秒 天 月 年
    NSUInteger unitFlags =
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
    
    //给出开始时间 和 结束时间 获取单位标识的数据
    NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:nowData toDate: endData options:0];
    NSInteger Hour = [cps hour];
    NSInteger Min = [cps minute];
    NSInteger Sec = [cps second];
    NSInteger Day = [cps day];
    NSInteger Mon = [cps month];
    NSInteger Year = [cps year];
    
    NSLog( @" From Now to %@, diff: Years: %ld Months: %ld, Days; %ld, Hours: %ld, Mins:%ld, sec:%ld",
          [nowData description], Year, Mon, Day, Hour, Min, Sec);
    NSString *countdown = [NSString stringWithFormat:@"还剩: %zi天 %zi小时 %zi分钟 %zi秒 ", Day,Hour, Min, Sec];
    if (Sec<0) {
        countdown=[NSString stringWithFormat:@"活动结束/开始抢购"];
    }
    return countdown;
} 
原文地址:https://www.cnblogs.com/xiaobai51/p/6285031.html