iOS NSDate 时间判断


项目里要根据时间戳计算时间是一周内,一天内,今天做下笔记,方便以后参考:


直接上函数:

#define DATE_TIME_MINUTEX    60
#define DATE_TIME_HOURS      (60*60)
#define DATE_ONETHOUSAND     1000
#define DATE_TIME_DAYTIME    (24*60*60)
#define DATE_TIME_DAYTIMES   (24*60*60*2)

/**
 * string类型时间戳转时间
 */
- (NSString *)intervalSinceNow: (NSString *) theDate
{
        NSTimeInterval late = [theDate floatValue]/DATE_ONETHOUSAND;
        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval now=[dat timeIntervalSince1970]*1;
        NSString *timeString=@"";
        NSTimeInterval cha=now-late;
        if (cha/DATE_TIME_HOURS<1) {
            timeString = [NSString stringWithFormat:@"%f", cha/DATE_TIME_MINUTEX];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
        }
        if (cha/DATE_TIME_HOURS>1 && cha/DATE_TIME_DAYTIME<1) {
            timeString = [NSString stringWithFormat:@"%f", cha/DATE_TIME_HOURS];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString=[NSString stringWithFormat:@"%@小时前", timeString];
        }
        if (cha/DATE_TIME_DAYTIME>1 && cha/DATE_TIME_DAYTIMES<1) {
            timeString=@"昨天";
        }
        if (cha/DATE_TIME_DAYTIMES>1)
        {
            timeString = [NSString stringWithFormat:@"%f", cha/DATE_TIME_DAYTIME];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString=[NSString stringWithFormat:@"%@天前", timeString];
        }
}



判断是否是本周和是否是今天的信息:

  

//判断是否是今天的消息
-(BOOL)isTodayNews: (NSString *) theDate
{
    NSTimeInterval late = [theDate floatValue]/DATE_ONETHOUSAND;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY/MM/dd"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:late];
    NSString *dateSMS = [dateFormatter stringFromDate:date];
    NSDate *now = [NSDate date];
    NSString *dateNow = [dateFormatter stringFromDate:now];
    if ([dateSMS isEqualToString:dateNow]) {
        return YES;
    }
    else {
        return NO;
    }
}

//判断是否是本周的消息
-(NSDate *)dateStartOfWeek:(NSDate *)date {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar] ;
    [gregorian setFirstWeekday:2]; //monday is first day
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date];
    NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init] ;
    [componentsToSubtract setDay: - ((([components weekday] - [gregorian firstWeekday])
                                      + 7 ) % 7)];
    NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:date options:0];
    NSDateComponents *componentsStripped = [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                                        fromDate: beginningOfWeek];
    
    //gestript
    beginningOfWeek = [gregorian dateFromComponents: componentsStripped];
    return beginningOfWeek;
}

不对的地方请大神指点。



你可能感兴趣的:(oc)