码迷,mamicode.com
首页 > 其他好文 > 详细

时间日期判断实例

时间:2015-08-10 23:40:45      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

直接看代码吧:

//  Created by cctv on 15/8/7.

//  Copyright (c) 2015年 cctv. All rights reserved.

//

 

#import "NSString+add.h"

#import "NSDate+add.h"

 

 

@implementation NSString (add)

+ (NSString *)stringWithDateString:(NSString*) dateString

{
     //  dateString -> @"Mon Nov 02 19:32:59 +0800 2015"

    NSDateFormatter *dateF = [[NSDateFormatter alloc] init];

    dateF.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";

    dateF.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-AS"];

   [dateF dateFromString:dateString];

    // 创建时间

    NSDate *date = [dateF dateFromString:dateString];

    //判断创建时间与当前时间的关系

    if (date.dateIsThisDay) {

        NSDateComponents *dateComp = [date dateIsToday];

        if (dateComp.hour >= 1) {

            dateF.dateFormat = [NSString stringWithFormat:@"%d小时前",dateComp.hour];

            return [dateF stringFromDate:date];

        }else if(dateComp.minute >= 1 ){

            dateF.dateFormat = [NSString stringWithFormat:@"%d分钟前",dateComp.minute];

            return [dateF stringFromDate:date];

        }else{

            return @"刚刚";

        }

    }else if (date.dateIsYesterDay){

        dateF.dateFormat = @"昨天 HH:mm";

        return [dateF stringFromDate:date];

    }else if(date.dateIsThisYear){

        dateF.dateFormat = @"MM-dd HH:mm";

        return [dateF stringFromDate:date];

    }else{

        dateF.dateFormat = @"yyyy-MM-dd HH:mm";

        return [dateF stringFromDate:date];

    }

}

 

 

@end

--------。。。。。------

#import "NSDate+add.h"

 

@implementation NSDate (add)

- (BOOL)dateIsThisYear

{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *nowComp = [calendar components:(NSCalendarUnitYear) fromDate:[NSDate date]];

    NSDateComponents *selfComp = [calendar components:(NSCalendarUnitYear) fromDate:self];

    return nowComp.year == selfComp.year;

}

- (BOOL)dateIsThisDay

{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *nowComp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];

    NSDateComponents *selfComp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];

    return nowComp.year == selfComp.year && nowComp.month == selfComp.month && nowComp.day == selfComp.day;

}

- (BOOL)dateIsYesterDay

{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *nowComp = [calendar components:(NSCalendarUnitYear) fromDate:[NSDate date]];

    NSDateComponents *selfComp = [calendar components:(NSCalendarUnitYear) fromDate:self];

    return nowComp.year == selfComp.year;

}

- (NSDateComponents*)dateIsToday

{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *dateComp = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:self toDate:[NSDate date] options:0];

       return dateComp;

}

 

@end

 

 

时间日期判断实例

标签:

原文地址:http://www.cnblogs.com/qq449832375/p/4719371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!