首页 > 技术文章 > 时间相关

z-z-z 2017-11-09 15:02 原文

1、获取距离当前时间的  时长

   NSString *newsDate = @"2017-11-30";
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSLog(@"newsDate = %@",newsDate);
    NSDate *newsDateFormatted = [dateFormatter dateFromString:newsDate];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    
    NSDate* current_date = [[NSDate alloc] init];
    NSTimeInterval time=[current_date timeIntervalSinceDate:newsDateFormatted];//间隔的秒数
    int month=((int)time)/(3600*24*30);   //距离几个月
    int day=((int)time)/(3600*24);      //距离几天
    int hour=((int)time)%(3600*24)/3600;   //距离几小时
    int minute=((int)time)%(3600*24)/60;   //距离几分钟

 

推荐阅读