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];
}


設(shè)置時(shí)區(qū)獲取準(zhǔn)確的時(shí)間
+ (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:@"月"];   
}


獲取時(shí)間描述
+ (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個(gè)月以前",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小時(shí)以前",ago];
    }
    
    if (ago > 1 && min < 0) {
        return [NSString stringWithFormat:@"%zd小時(shí)以前",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;
}




最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容