美文网首页
字符串转成NSDate类型,计算与当前时间的相差,年数,天数,时

字符串转成NSDate类型,计算与当前时间的相差,年数,天数,时

作者: 呦嚯嚯嚯12138 | 来源:发表于2016-10-03 16:03 被阅读33次

    NSString *dateStr=@"2013-08-13 20:28:40";//传入时间

    //将传入时间转化成需要的格式

    NSDateFormatter *format=[[NSDateFormatter alloc] init];

    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate *fromdate=[format dateFromString:dateStr];

    NSTimeZone *fromzone = [NSTimeZone systemTimeZone];

    NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];

    NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];

    NSLog(@"fromdate=%@",fromDate);

    [format release];

    //获取当前时间

    NSDate *date = [NSDate date];

    NSTimeZone *zone = [NSTimeZone systemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: date];

    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];

    NSLog(@"enddate=%@",localeDate);

    double intervalTime = [fromDate timeIntervalSinceReferenceDate] - [localeDate timeIntervalSinceReferenceDate];

    long lTime = (long)intervalTime;

    NSInteger iSeconds = lTime % 60;

    NSInteger iMinutes = (lTime / 60) % 60;

    NSInteger iHours = (lTime / 3600)$;

    NSInteger iDays = lTime/60/60/24;

    NSInteger iMonth = lTime/60/60/24/12;

    NSInteger iYears = lTime/60/60/24/384;

    NSLog(@"相差M年d月 或者 d日d时d分d秒", iYears,iMonth,iDays,iHours,iMinutes,iSeconds);

    相关文章

      网友评论

          本文标题:字符串转成NSDate类型,计算与当前时间的相差,年数,天数,时

          本文链接:https://www.haomeiwen.com/subject/ackjyttx.html