美文网首页
让时间不再随系统设置而改变

让时间不再随系统设置而改变

作者: 九天环佩 | 来源:发表于2016-07-15 21:20 被阅读33次

    + (NSString *)timeByTimestamp:(CGFloat)timestamp {
    
        NSTimeZone *zone = [NSTimeZone defaultTimeZone];
    
        static NSDateFormatter *formatter =nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            formatter = [[NSDateFormatter alloc] init];
        });
        formatter.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
        [formatter setDateFormat:@"yyyy-MM-dd aa hh:mm"];
        [formatter setTimeZone:zone];
    
        CGFloat thatTime = timestamp;
        NSDate *thatDate = [NSDate dateWithTimeIntervalSince1970:thatTime];
        NSString *thatString = [formatter stringFromDate:thatDate];
        NSDate *nowDate = [NSDate date];
        CGFloat nowTime = [nowDate timeIntervalSince1970];
    
        CGFloat spaceTime = nowTime - thatTime;
    
        if (spaceTime / 60 < 5) {
            return @"刚刚";
        } else if (spaceTime < 60 * 60 * 24) {
            NSString *time = [thatString substringFromIndex:11];
            if ([time hasPrefix:@"PM"]) {
                time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"下午"];
            }
            if ([time hasPrefix:@"AM"]) {
                time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"上午"];
            }
            return time;
        } else if (spaceTime < 60 * 60 * 24 * 2) {
            NSString *time = [thatString substringFromIndex:11];
            if ([time hasPrefix:@"PM"]) {
                time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"昨天下午"];
            }
            if ([time hasPrefix:@"AM"]) {
                time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"昨天上午"];
            }
            return time;
        } else if (spaceTime < 60 * 60 * 24 * 3) {
            NSString *time = [thatString substringFromIndex:11];
            if ([time hasPrefix:@"PM"]) {
                time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"前天下午"];
            }
            if ([time hasPrefix:@"AM"]) {
                time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"前天上午"];
            }
            return time;
        } else if (spaceTime < 60 * 60 * 24 * 4) {
            NSString *time = [thatString substringFromIndex:11];
            if ([time hasPrefix:@"PM"]) {
                time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"大前天下午"];
            }
            if ([time hasPrefix:@"AM"]) {
                time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"大前天上午"];
            }
            return time;
        } else if (spaceTime < 60 * 60 * 24 * 365) {
            NSDateFormatter *dateFor = [[NSDateFormatter alloc] init];
            [dateFor setDateFormat:@"M月d日 aa h:mm"];
            [dateFor setTimeZone:zone];
            NSString *time = [dateFor stringFromDate:thatDate];
            time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"下午"];
            time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"上午"];
            return time;
        } else {
            NSDateFormatter *dateFor = [[NSDateFormatter alloc] init];
            [dateFor setDateFormat:@"yyyy年M月d日 aa h:mm"];
            [dateFor setTimeZone:zone];
            NSString *time = [dateFor stringFromDate:thatDate];
            time = [time stringByReplacingOccurrencesOfString:@"PM" withString:@"下午"];
            time = [time stringByReplacingOccurrencesOfString:@"AM" withString:@"上午"];
            return time;
        }
    }
    



    相关文章

      网友评论

          本文标题:让时间不再随系统设置而改变

          本文链接:https://www.haomeiwen.com/subject/npnyjttx.html