iOS开发--时间与日期处理

在日常开发工作中经常需要对时间与日期进行处理,一般情况下后台返回的是时间戳(timeStamp)的形式,需要前端加以处理后再展示,后台返回的timeStamp 有些时候是精确到秒(一般leng == 10)有些时候是精确到毫秒(一般leng == 13) ,网上有一些时间戳的在线转换工具,例如 Unix时间戳在线转换工具

时间戳转化为时间NSDate(直接返回指定格式的日期)
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString {
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
    //格式显示类型有: @"M/d/yy" -> 12/7/58,  @"d-MMM" -> 7-Dec, @"d-MMMM-yy" -> 7-December-58, @"d MMMM" -> 7 December, @"MMMM yy" -> December 58, @"hh:mm tt" -> 08:50 PM, @"h:mm:ss t" -> :50:35 P, @"H:mm" -> 20:50 ,@"H:mm:ss" -> 20:50:35, @"M/d/yyyy H:mm" -> 12/7/1958 20:50 ,
    
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue] / 1000.0];// 毫秒值转化为秒 / 1000.0  否则为精确到秒
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
时间戳转化为时间NSDate(在返回指定格式日期的基础上判断"刚刚","几分钟前","几小时前")
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString {
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM-dd"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];// 毫秒值转化为秒 / 1000.0  否则为精确到秒
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:date];
    long temp = 0;
    NSString *result;
    if (timeInterval/60 < 1) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }else{
        NSString *dateString = [formatter stringFromDate:date];
        return dateString;
    }
    return result;
}
时间转化为时间戳
- (NSString *)timeWithDate {
    // 当前时间
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a = [date timeIntervalSince1970] * 1000.0; // *1000 是精确到毫秒,不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
    return timeString;
}
根据时间戳获取星期几
+ (NSString *)getWeekDayFordate:(long long)data
{
    NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];

    NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:data];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:newDate];

    NSString *weekStr = [weekday objectAtIndex:components.weekday];
    return weekStr;
}
根据NSString类型时间戳转换为日期格式
+ (NSString *)getDateAccordingTime:(NSString *)aTime formatStyle:(NSString *)formate{

    NSDate *nowDate = [NSDate dateWithTimeIntervalSince1970:[aTime intValue]];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:formate];
    return[formatter stringFromDate:nowDate];
}

本文部分内容参考自 时间与日期处理

你可能感兴趣的:(iOS开发--时间与日期处理)