【原】iOS学习之NSDate在项目中的一些类目扩展

在项目中,我们可能会面对各种各样的对于时间的需求,在这里提供几种可能会用到的需求代码

1、与今天的时间做比较,返回日期差值

代码:

- (NSInteger)compareWithToday {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *today = [NSDate date];    
    NSString *todayStr = [dateFormatter stringFromDate:today];
    today = [dateFormatter dateFromString:todayStr];
    
    NSInteger interval = (NSInteger) [self timeIntervalSinceDate:today];
    
    NSInteger intervalDate = 0;
    if (interval <= 0) {
        intervalDate = interval / (24 * 60 * 60) - 1;
    } else {
        intervalDate = interval / (24 * 60 * 60);
    }
    return intervalDate;
}

2、距离当前的时间间隔描述

代码:

- (NSString *)timeIntervalDescription
{
    NSTimeInterval timeInterval = -[self timeIntervalSinceNow];
    if (timeInterval < 60) {
        return @"1分钟内";
    } else if (timeInterval < 3600) {
        return [NSString stringWithFormat:@"%.f分钟前", timeInterval / 60];
    } else if (timeInterval < 86400) {
        return [NSString stringWithFormat:@"%.f小时前", timeInterval / 3600];
    } else if (timeInterval < 2592000) {//30天内
        return [NSString stringWithFormat:@"%.f天前", timeInterval / 86400];
    } else if (timeInterval < 31536000) {//30天至1年内
        NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterWithFormat:@"M月d日"];
        return [dateFormatter stringFromDate:self];
    } else {
        return [NSString stringWithFormat:@"%.f年前", timeInterval / 31536000];
    }
}

 该方法主要用于计算时间距离当前时间的时间间隔描述,主要使用时间戳进行比较。思路是先计算出时间戳与当前时间的时间戳的差值,然后进行比较。

3、分解时间

代码:

#define DATE_COMPONENTS (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfYear |  NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal)
#define CURRENT_CALENDAR [NSCalendar currentCalendar]

- (NSInteger) nearestHour
{
    NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_MINUTE * 30;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    NSDateComponents *components = [CURRENT_CALENDAR components:NSCalendarUnitHour fromDate:newDate];
    return components.hour;
}

- (NSInteger) hour
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    return components.hour;
}

- (NSInteger) minute
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    return components.minute;
}

- (NSInteger) seconds
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    return components.second;
}

- (NSInteger) day
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    return components.day;
}

- (NSInteger) month
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    return components.month;
}

- (NSInteger) week
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    return components.weekOfMonth;
}

- (NSInteger) weekday
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    return components.weekday;
}

- (NSInteger) nthWeekday // e.g. 2nd Tuesday of the month is 2
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    return components.weekdayOrdinal;
}

- (NSInteger) year
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    return components.year;
}

 这些方法主要是用于将时间中某一个单位下的内容以数字的形式返回。

 比如 2016-08-26 08:55:48 +0000 是零时区下的当前时间,

  使用 - (NSInteger) year  方法的输出结果就是 2016;

  使用 - (NSInteger) hour  方法的输出结果就是 当前时区的时间,我们在 8时区 ,结果就是 16;

  使用 - (NSInteger) seconds  方法的输出结果就是 48 ...

原文地址:https://www.cnblogs.com/gfxxbk/p/5810945.html