NSDate、NSDateFormatterd、NSDateComponents的使用

// "2017-07-14 15:54:26"

+ (NSInteger)getDay:(NSString *)originDateStr {
    
    originDateStr = @"2013-12-14 05:54:26";
    
    NSDate *date = [self getDateFromStr:originDateStr];
    
    NSDateComponents *components = [self getDateComponents:date];

    components.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    
    return [components day];
}


设置时区获取准确的时间
+ (NSString *)getMonth_zh:(NSString *)originDateStr {
    
    originDateStr = @"2013-12-14 05:54:26";
    
    NSDate *date = [self getDateFromStr:originDateStr];
    
    NSDateComponents *components = [self getDateComponents:date];
    
    components.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    
    NSInteger month = [components month];
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
    formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
    
    NSString *monthStr = [formatter stringFromNumber:[NSNumber numberWithInt: (int)month]];
    
    return [monthStr stringByAppendingString:@"月"];   
}


获取时间描述
+ (NSString *)getTimeDetail:(NSString *)originDateStr {
    
    originDateStr = @"2017-09-15 09:54:26";
    
    NSDate *date = [self getDateFromStr:originDateStr];
    
    NSDateComponents *components = [self getDateComponents:date];
    
    NSDate *nowDate = [NSDate date];
    
    NSDateComponents *nowComponents = [self getDateComponents:nowDate];
    
    NSComparisonResult result = [date compare:nowDate];
    
    if (result != NSOrderedAscending) {
        return @"刚刚";
    }
    
    NSInteger ago = [nowComponents year] - [components year];
    if (ago > 0) {
        return [NSString stringWithFormat:@"%zd年以前",ago];
    }
    
    ago = [nowComponents month] - [components month];
    if (ago > 0) {
        return [NSString stringWithFormat:@"%zd个月以前",ago];
    }
    
    ago = [nowComponents day] - [components day];
    if (ago > 0) {
        return [NSString stringWithFormat:@"%zd天以前",ago];
    }
    
    ago = [nowComponents hour] - [components hour];
    NSInteger min =  [nowComponents minute] - [components minute];
    
    
    if (ago >= 1 && min >= 0) {
        return [NSString stringWithFormat:@"%zd小时以前",ago];
    }
    
    if (ago > 1 && min < 0) {
        return [NSString stringWithFormat:@"%zd小时以前",ago-1];
    }
    
    if (ago == 1 && min < 0) {
        return [NSString stringWithFormat:@"%zd分钟以前",min+60];
    }
    
    ago = [nowComponents minute] - [components minute];
    if (ago > 0) {
        return [NSString stringWithFormat:@"%zd分钟以前",ago];
    }

    return @"刚刚";
}


// string convert to date
+ (NSDate *)getDateFromStr:(NSString *)dateStr {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:[NSLocale currentLocale].localeIdentifier];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [dateFormatter dateFromString:dateStr];

    return date;
}
+ (NSDateComponents *)getDateComponents:(NSDate *)date {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:date];
    return components;
}




你可能感兴趣的:(NSDate、NSDateFormatterd、NSDateComponents的使用)