iOS常用方法——NSDate常见处理的方法封装

NSDate转换为时间戳

+(NSString *)dateChangeToTimestamp:(NSDate *)date{

    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    return timeSp;

}

时间戳转换为NSDate

+(NSDate *)timestampChangeToDate:(double)timestamp{
    return [NSDate dateWithTimeIntervalSince1970:timestamp];
}

时间戳转换为NSString

+(NSString *)timestampChangeToDateString:(double)timestamp{

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    /*
     日期格式:
     年月日:yyyy-MM-dd
     时分秒:HH:mm:ss
     yyyy-MM-dd HH:mm:ss
     yyyy-MM-dd HH:mm:ss zzz
     */
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [dateformatter stringFromDate:date];
    return dateString;
}

时间戳转化为需要自定义显示的格式

+(NSString *)getTimeWithTimestamp:(double)timestamp{
    NSDate * date = [NSString timestampChangeToDate:timestamp];
    NSDate * now = [NSDate date];
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy"];
    if ([[formatter stringFromDate:now] intValue] > [[formatter stringFromDate:date] intValue]) {
        [formatter setDateFormat:@"yyyy-MM-dd"];
        return [formatter stringFromDate:date];
    }
    [formatter setDateFormat:@"MM"];
    if ([[formatter stringFromDate:now] intValue] > [[formatter stringFromDate:date] intValue]) {
        [formatter setDateFormat:@"MM-dd HH:mm"];
        return [formatter stringFromDate:date];
    }
    [formatter setDateFormat:@"dd"];
    int days = [[formatter stringFromDate:now] intValue] - [[formatter stringFromDate:date] intValue];
    if (days >= 2) {
        [formatter setDateFormat:@"MM-dd HH:mm"];
        return [formatter stringFromDate:date];
    }
    else if (days >= 1){
        [formatter setDateFormat:@"HH:mm"];
        return  [NSString stringWithFormat:@"昨天%@",[formatter stringFromDate:date]];
    }
    else {
        [formatter setDateFormat:@"HH:mm"];
        return  [NSString stringWithFormat:@"今天%@",[formatter stringFromDate:date]];
    }
    return @"";

}

时间差转换为视频时长的显示格式:

+(NSString *)videoRecordTime:(int)recordTime{
    if (recordTime < 10) {
        return [NSString stringWithFormat:@"0:0%d",recordTime];
    }else if (recordTime < 60){
        return [NSString stringWithFormat:@"0:%d",recordTime];
    }else{
        if (recordTime%60 < 10) {
            return [NSString stringWithFormat:@"%d:0%d",recordTime/60,recordTime%60];
        }else{
            return [NSString stringWithFormat:@"%d:%d",recordTime/60,recordTime%60];
        }
    }
}

判断某个时间是否在某个时间段之间

+(BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour {

    NSDate *dateFrom = [NSString getCustomDateWithHour:fromHour];
    NSDate *dateTo = [NSString getCustomDateWithHour:toHour];

    NSDate *currentDate = [NSDate date];
    if ([currentDate compare:dateFrom]==NSOrderedDescending && [currentDate compare:dateTo]==NSOrderedAscending) {
        return YES;
    }
    return NO;
}

+(NSDate *)getCustomDateWithHour:(NSInteger)hour {
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *currentComps = [[NSDateComponents alloc] init];

    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

    currentComps = [currentCalendar components:unitFlags fromDate:currentDate];

    //设置当天的某个点
    NSDateComponents *resultComps = [[NSDateComponents alloc] init];
    [resultComps setYear:[currentComps year]];
    [resultComps setMonth:[currentComps month]];
    [resultComps setDay:[currentComps day]];
    [resultComps setHour:hour];

    NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [resultCalendar dateFromComponents:resultComps];
}

NSDate转换为年龄

+(NSString *)dateToAge:(NSDate *)bornDate{
    //获得当前系统时间
    NSDate *currentDate = [NSDate date];
    //获得当前系统时间与出生日期之间的时间间隔
    NSTimeInterval time = [currentDate timeIntervalSinceDate:bornDate];
    //时间间隔以秒作为单位,求年的话除以60*60*24*356
    int age = ((int)time)/(3600*24*365);
    return [NSString stringWithFormat:@"%d",age];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值