IOS-计算某个时间与当前时间的时间差

//计算日期
-(NSInteger)dateRemaining:(NSString *)Date{
    //日期格式设置,可以根据想要的数据进行修改 添加小时和分甚至秒
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    //得到时区,根据手机系统时区设置(systemTimeZone)
    NSTimeZone *zone=[NSTimeZone systemTimeZone];

    //获取当前日期
    NSDate *nowDate=[NSDate date];

    /*GMT:格林威治标准时间*/
    //格林威治标准时间与系统时区之间的偏移量(秒)
    NSInteger nowInterval=[zone secondsFromGMTForDate:nowDate];

    //将偏移量加到当前日期
    NSDate *nowTime=[nowdate dateByAddingTimeInterval:nowInterval];
    
    //传入日期设置日期格式
    NSDate *yourDate = [dateFormatter dateFromString:Date];

    //格林威治标准时间与传入日期之间的偏移量
    NSInteger yourInterval = [zone secondsFromGMTForDate:yourDate];

    //将偏移量加到传入日期
    NSDate *yourTime = [yourDate dateByAddingTimeInterval:yourInterval];
    
    //time为两个日期的相差秒数
    NSTimeInterval time=[yourTime timeIntervalSinceDate:nowTime];

    //最后通过秒数time计算所剩时间 几年?几月?几天?几时?几秒?
    time = time/(3600*24);
    return (NSInteger)time;
}

猜你喜欢

转载自blog.csdn.net/qq_36557133/article/details/81229863