博客> 时间,时间戳
时间,时间戳
2018-09-21 16:26 评论:0 阅读:426 易_君
ios oc 时间戳 时间 时间计算

pragma mark -- 获取时间

  • (NSString )getTime { NSDate date = [NSDate date]; NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"yyyy-MM-dd HH:mm:ss";

    return [format stringFromDate:date]; }

pragma mark -- 时间戳

  • (NSString )getTimeStamp:(NSString )timeStampString { NSDate date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue]]; NSDateFormatter objDateformat = [[NSDateFormatter alloc] init]; [objDateformat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSLog(@"%@", [objDateformat stringFromDate:date]); NSString *time = [objDateformat stringFromDate:date]; return [NSString stringWithFormat:@"%@",time]; }

pragma mark -- 转时间戳

  • (NSString )timeInterval:(NSString )timeStr { NSDateFormatter format = [[NSDateFormatter alloc] init]; format.dateFormat = @"yyyy-MM-dd, hh:mm:ss"; NSDate date=[format dateFromString:timeStr]; //时间date转时间戳 NSTimeInterval time = [date timeIntervalSince1970]; NSString *timeString = [NSString stringWithFormat:@"%.0f", time];

    return timeString; }

pragma mark -- 计算时间间隔

  • (NSString )calDateInterval:(NSString )startTime { NSDate nowDate = [NSDate date]; // 当前日期 NSDateFormatter formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate startData = [formatter dateFromString:startTime];// 将传入的字符串转化成时间 NSTimeInterval delta = [startData timeIntervalSinceDate:nowDate]; // 计算出相差多少秒 //打印结果格式为 delta ==== 181078.541819 NSString during = [NSString stringWithFormat:@"%.0lf",delta]; NSLog(@"时间差:%@",during); return during; }

  • (NSString )CalDateInterval:(NSString )startTime { NSDateFormatter inputFormatter = [[NSDateFormatter alloc] init]; [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate startDate = [inputFormatter dateFromString:startTime];

    NSDate *nowDate = [NSDate date];

    NSString *strResult = nil;

    NSCalendar chineseClendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 当前日历 // NSCalendar calendar = [NSCalendar currentCalendar];

    // 需要对比的时间数据 NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; // 对比时间差 NSDateComponents *DateComponent = [chineseClendar components:unit fromDate:startDate toDate:nowDate options:0];

    NSInteger diffHour = [DateComponent hour];

    NSInteger diffMin = [DateComponent minute];

    NSInteger diffSec = [DateComponent second];

    NSInteger diffDay = [DateComponent day];

    NSInteger diffMon = [DateComponent month];

    NSInteger diffYear = [DateComponent year];

    if (diffYear > 0) { strResult=[NSString stringWithFormat:@"%ld 年前",(long)diffYear]; }else if(diffMon > 0){ strResult=[NSString stringWithFormat:@"%ld 月前",(long)diffMon]; }else if(diffDay > 0){ strResult=[NSString stringWithFormat:@"%ld 天前",(long)diffDay]; }else if(diffHour > 0){ strResult=[NSString stringWithFormat:@"%ld 小时前",(long)diffHour]; }else if(diffMin > 0){ strResult=[NSString stringWithFormat:@"%ld 分钟前",(long)diffMin]; }else if(diffSec > 0){ strResult=[NSString stringWithFormat:@"%ld 秒前",(long)diffSec]; }else{ strResult=[NSString stringWithFormat:@"未知时间"]; }

    if (diffHour > 0 && diffMin > 0 && diffSec >= 0) { strResult = [NSString stringWithFormat:@"%ld",diffHour 360 + diffMin + diffSec]; }else if (diffHour == 0 && diffMin >= 0 && diffSec >= 0) { strResult = [NSString stringWithFormat:@"%ld",diffMin 60 + diffSec]; }else if (diffHour > 0 && diffMin == 0 && diffSec >= 0) { strResult = [NSString stringWithFormat:@"%ld",diffHour * 360 + diffSec]; }else if (diffHour == 0 && diffMin == 0 && diffSec > 0) { strResult = [NSString stringWithFormat:@"%ld",diffSec]; }else{ strResult=[NSString stringWithFormat:@"1"]; }

    strResult = [NSString stringWithFormat:@"%ld",diffHour 360 + diffMin60 + diffSec];

    return strResult; }

/**

  • 计算剩余时间
  • @param endTime 结束日期
  • @return 剩余时间 */

    • (NSString )getCountDownStringWithEndTime:(NSString )endTime {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    NSDate now = [NSDate date]; NSTimeZone zone = [NSTimeZone systemTimeZone];//设置时区 NSInteger interval = [zone secondsFromGMTForDate:now]; NSDate localDate = [now dateByAddingTimeInterval:interval]; endTime = [NSString stringWithFormat:@"%@", 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:@"%ld", voteCountTime];

    return timeStr; }

pragma mark -- 一个时间距现在的时间

  • (NSString )intervalSinceNow:(NSString )theDate { NSDateFormatter date=[[NSDateFormatter alloc] init]; [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate d=[date dateFromString:theDate];

    NSTimeInterval late=[d timeIntervalSince1970]*1;

    NSDate dat = [NSDate date]; NSTimeInterval now=[dat timeIntervalSince1970]1; NSString *timeString=@"";

    NSTimeInterval cha=late-now;

    if (cha/3600<1 xss=removed xss=removed xss=removed>1&&cha/86400<1 xss=removed xss=removed timeString=[NSString>1) { timeString = [NSString stringWithFormat:@"%f", cha/86400]; timeString = [timeString substringToIndex:timeString.length-7]; timeString=[NSString stringWithFormat:@"剩余%@天", timeString];

    } return timeString; }

收藏
0
sina weixin mail 回到顶部