固予IOSiOS开发知识收录

ios 获取当前时间、一周前时间、比较时间大小、时间戳转换

2016-07-12  本文已影响6392人  来宝

1、获取当前日期、时间

    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate:currentDate];

2、获取一周前的日期、时间

    NSDate * date = [NSDate date];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //一周的秒数
    NSTimeInterval time = 7 * 24 * 60 * 60;
    //下周就把"-"去掉
    NSDate *lastWeek = [date dateByAddingTimeInterval:-time];
    NSString *startDate =  [dateFormatter stringFromDate:lastWeek];

3、比较两个日期大小

//比较两个日期大小
-(int)compareDate:(NSString*)startDate withDate:(NSString*)endDate{
    
    int comparisonResult;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date1 = [[NSDate alloc] init];
    NSDate *date2 = [[NSDate alloc] init];
    date1 = [formatter dateFromString:startDate];
    date2 = [formatter dateFromString:endDate];
    NSComparisonResult result = [date1 compare:date2];
    NSLog(@"result==%ld",(long)result);
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending:
            comparisonResult = 1;
            break;
            //date02比date01小
        case NSOrderedDescending:
            comparisonResult = -1;
            break;
            //date02=date01
        case NSOrderedSame:
            comparisonResult = 0;
            break;
        default:
            NSLog(@"erorr dates %@, %@", date1, date2);
            break;
    }
    return comparisonResult;
}
 int comparisonResult = [self compareDate:startDate withDate:endDate];
if(comparisonResult >0){
  //endDate 大
}

4、比较日期差

//比较两个日期大小
-(NSInteger)compare:(NSDate *)startTime to:(NSDate *)endTime{

    // 当前日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 需要对比的时间数据
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
    | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 对比时间差
    NSDateComponents *dateCom = [calendar components:unit fromDate:startTime toDate:endTime options:0];
    NSString *time = [NSString stringWithFormat:@"%ld%ld%ld%ld%ld%ld",dateCom.year,dateCom.month,dateCom.day,dateCom.hour,dateCom.minute,dateCom.second];
    NSLog(@"time---->%@",time);
    
    return [time integerValue];
}

5、时间和时间戳的转换

   //获取系统时间戳
    NSDate* date1 = [NSDate date];
    NSTimeInterval time1 =[date1 timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f",time1];
    NSLog(@"系统时间戳:%@",timeString);

    //时间戳转换成时间
    NSTimeInterval time2 =[timeString doubleValue];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:time2];
    NSLog(@"date2 = %@",date2);
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy/MM/dd"];
    NSString *currentTime = [formatter stringFromDate:date2];
    NSLog(@"当前时间:%@",currentTime);
    //时间转时间戳
    NSString *timeStr = [NSString stringWithFormat:@"%.0f",[date2 timeIntervalSince1970]];
    NSLog(@"时间戳:%@",timeStr);
上一篇下一篇

猜你喜欢

热点阅读