iOS 时间日期的处理

注:以下方法都是写在NSDate的分类中

一 获取当前时间的时间戳

  • 获取当前时间戳(以秒为单位)
+(NSString *)getNowTimeSecondTimestamp{
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
    return timeSp;
}
  • 获取当前时间戳(以毫秒为单位)
+(NSString *)getNowTimeMillisecondTimestamp{
    NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
    [formatter setDateFormat:@"SSS"];
    NSString *sssStr = [formatter stringFromDate:[NSDate date]];
    NSTimeInterval  resultSSS= [[NSDate date] timeIntervalSince1970]*1000+ sssStr.integerValue;
    return [NSString stringWithFormat:@"%.0f",resultSSS];
}

二 评论模块时间的显示

  • 宏定义
#define G_MINUTE    60
#define G_HOUR      3600
#define G_DAY       86400
#define G_WEEK      604800
#define G_YEAR      31556926
  • 需求一:
  • 1分钟内显示为“刚刚”,
  • 大于1分钟小于1小时显示为“n分钟前”,
  • 大于1小时小于一天显示为“n小时前”,
  • 大于1天小于7天显示为“n天前”,其他时间显示年月日
//后端返回的是毫秒时间戳
+ (NSString *)dateStyleCommentProductManagerTimeIntervalToDateString:(NSString *)timeInterval{
    if (timeInterval.length<10) {
        return @"";
    }
    //转化为秒单位时间戳
    NSMutableString  *mutabletimeInterval=[[NSMutableString alloc]initWithString:timeInterval];
    [mutabletimeInterval insertString:@"." atIndex:10];
    NSTimeInterval timeInter = mutabletimeInterval.doubleValue;
    
    NSTimeInterval timeNowInter = [NSDate getNowTimeSecondTimestamp].doubleValue;
    NSTimeInterval timeDvalue = timeNowInter-timeInter;
    
    NSString *dateString = [NSDate dateTimeIntervalToDateString:timeInterval];
    
    if (timeNowInter-timeInter=G_MINUTE)&&(timeDvalue=G_HOUR)&&(timeDvalue=G_DAY)&&(timeDvalue
  • 需求二:
  1. 今年以前显示"年月日"
  2. 今年今天
    2.1 一分钟内显示为“刚刚”,
    2.2 大于1分钟小于1小时显示为“n分钟前”,
    2.3 大于1小时小于一天显示为“n小时前”,
  3. 今年昨天显示"昨天"
  4. 今年昨天以前显示"月日"
+ (NSString *)dateStyleCommentTimeIntervalToDateString:(NSString *)timeInterval{
    if (timeInterval.length<10) {
        return @"";
    }
    NSMutableString  *mutabletimeInterval=[[NSMutableString alloc]initWithString:timeInterval];
    [mutabletimeInterval insertString:@"." atIndex:10];
    NSTimeInterval timeInter = mutabletimeInterval.doubleValue;
    
    NSDate *createDate = [NSDate dateWithTimeIntervalSince1970:timeInter];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";// HH:mm:ss
    NSString *dateStr = [formatter stringFromDate:createDate];
    NSDateComponents *detalCmp = [createDate detalDateWithNow];
    
    if ([createDate isThisYear]) {
        if ([createDate isToday]) {
            if (detalCmp.hour >= 1) { // 大于1小时
                dateStr = [NSString stringWithFormat:@"%ld小时前",(long)detalCmp.hour];
            } else if (detalCmp.minute > 1) { // 大于1分钟
                dateStr = [NSString stringWithFormat:@"%ld分钟前",(long)detalCmp.minute];
            } else { // 刚刚
                dateStr = @"刚刚";
            }
        } else if ([createDate isYesterday]) { //昨天
            formatter.dateFormat = @"昨天";
            dateStr = [formatter stringFromDate:createDate];
        } else { // 昨天之前
            formatter.dateFormat = @"MM-dd";
            dateStr = [formatter stringFromDate:createDate];
        }
    }
    return dateStr;
}
- (NSDateComponents *)detalDateWithNow{
    // 判断下发布日期 与 当前日期 小时,分差值
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSInteger unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    // 获取两个日期的差值,获取发布日期与当前日期差值
    return [currentCalendar components:unit  fromDate:self toDate:[NSDate date] options:NSCalendarWrapComponents];
}
- (BOOL) isThisYear{
    NSDateComponents *components1 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:self];
    NSDateComponents *components2 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:[NSDate date]];
    return (components1.year == components2.year);
}
- (BOOL) isToday{
    NSDateComponents *components1 = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    NSDateComponents *components2 = [[NSDate currentCalendar] components:componentFlags fromDate:[NSDate date]];
    return ((components1.year == components2.year) &&
            (components1.month == components2.month) &&
            (components1.day == components2.day));
}
- (BOOL) isYesterday{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setDay:-1];
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
    
    NSDateComponents *components1 = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    NSDateComponents *components2 = [[NSDate currentCalendar] components:componentFlags fromDate:newDate];
    return ((components1.year == components2.year) &&
            (components1.month == components2.month) &&
            (components1.day == components2.day));
}
+ (NSCalendar *) currentCalendar{
    static NSCalendar *sharedCalendar = nil;
    if (!sharedCalendar)
        sharedCalendar = [NSCalendar autoupdatingCurrentCalendar];
    return sharedCalendar;
}
static const unsigned componentFlags = (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfMonth |  NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal);

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