ios 日期操作封装

/**

 *  获取明天日期

 *

 *  @return NSDate类型

 */

+(NSDate *) dateTomorrow

{

    return [[NSDate date] initWithTimeIntervalSinceNow:24 * 60 * 60 ];

}


/**

 *  获取昨天日期

 *

 *  @return NSDate类型

 */

+(NSDate *) dateYesterday

{

    return [[NSDate date] initWithTimeIntervalSinceNow: -24 * 60 * 60];

}


/**

 *  获取指定天数后的日期

 *

 *  @param days 天数

 *

 *  @return  NSDate类型

 */

+(NSDate *) dateWithDaysFromNow:(NSUInteger) days

{

    return [[NSDate date] initWithTimeIntervalSinceNow:days * 24 * 60 * 60];

}


/**

 *  获取指定天数前的日期

 *

 *  @param days 天数

 *

 *  @return NSDate类型

 */

+(NSDate *) dateWithDaysBeforeNow:(NSInteger) days

{

    return [[NSDate date] initWithTimeIntervalSinceNow:((-24) * 60 * 60 * days)];

}


/**

 *  获取指定小时数后的日期

 *

 *  @param dHours 小时数

 *

 *  @return NSDate类型

 */

+(NSDate *) dateWithHoursFromNow:(NSUInteger) dHours

{

    return [[NSDate date] initWithTimeIntervalSinceNow:dHours * 60 * 60];

}


/**

 *  获取指定小时数前的日期

 *

 *  @param dHours 小时数

 *

 *  @return NSDate类型

 */

+(NSDate *) dateWithHoursBeforeNow:(NSInteger) dHours

{

    return [[NSDate date] initWithTimeIntervalSinceNow:dHours * 60 * 60 * (-1)];

}


/**

 *  获取指定分钟后的日期

 *

 *  @param dMinutes 分钟

 *

 *  @return NSDate类型

 */

+(NSDate *) dateWithMinutesFromNow:(NSUInteger) dMinutes

{

    return [[NSDate date] initWithTimeIntervalSinceNow:dMinutes * 60];

}


/**

 *  获取指定分钟前的日期

 *

 *  @param dMinutes 分钟

 *

 *  @return NSDate类型

 */

+(NSDate *) dateWithMinutesBeformNow:(NSInteger) dMinutes

{

    return [[NSDate date] initWithTimeIntervalSinceNow:dMinutes * 60 * (-1)];

}


/**

 *  当前日期与指定日期比较是否相等

 *

 *  @param dMinutes 分钟

 *

 *  @return NSDate类型

 */

-(BOOL) isEqualtoDatelgnoringTime:(NSDate *) aDate

{

    return [[NSDate date] isEqualToDate:aDate];

}


/**

 *  是不是今天

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) IsToday:(NSDate *) aDate

{

    NSCalendar *cld = [[NSCalendar alloc] init];

    return [cld isDateInToday:aDate];

}


/**

 *  是不是明天

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isTomorrow:(NSDate *) aDate;

{

    NSCalendar *cld = [[NSCalendar alloc] init];

    return [cld isDateInTomorrow:aDate];

}


/**

 *  是不是昨天

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isYesterday:(NSDate *) aDate

{

    NSCalendar *cld = [[NSCalendar alloc] init];

    return [cld isDateInYesterday:aDate];

}


/**

 *  是不是同一周

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isSameWeekAsDate:(NSDate *) aDate

{

    NSDate *start;

    NSTimeInterval extends;

    NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];

    NSDate *today = [NSDate date];

    BOOL success = [cal rangeOfUnit:NSWeekdayCalendarUnit startDate:&start interval:&extends forDate:today];

    

    if (!success) {

        return false;

    }

    

    NSTimeInterval dateInSecs = [aDate timeIntervalSinceReferenceDate];

    NSTimeInterval dateStartInSecs = [start timeIntervalSinceReferenceDate];

    if (dateInSecs > dateStartInSecs && dateInSecs < (dateStartInSecs + extends)) {

        return true;

    }else {

        return false;

    }

}


/**

 *  是不是同一周

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isThisWeek:(NSDate *) aDate

{

    NSDate *start;

    NSTimeInterval extends;

    NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];

    NSDate *today = [NSDate date];

    // 根据参数提供的时间点,返回所在日历单位的开始时间。如果startDateinterval均可以计算,则返回YES;否则返回NO

    // extends获取的是日历单位所对应的秒数

    BOOL success = [cal rangeOfUnit:NSWeekdayCalendarUnit startDate:&start interval:&extends forDate:today];

    

    if (!success) {

        return false;

    }

    

    NSTimeInterval dateInSecs = [aDate timeIntervalSinceReferenceDate];

    NSTimeInterval dateStartInSecs = [start timeIntervalSinceReferenceDate];

    if (dateInSecs > dateStartInSecs && dateInSecs < (dateStartInSecs + extends)) {

        return true;

    }else {

        return false;

    }

}


/**

 *  是不是下一周

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isNextWeek:(NSDate *) aDate

{

    NSDate *start;

    NSTimeInterval extends;

    NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];

    NSDate *today = [NSDate date];

    BOOL success = [cal rangeOfUnit:NSWeekdayCalendarUnit startDate:&start interval:&extends forDate:today];

    

    if (!success) {

        return false;

    }

    

    NSTimeInterval dateInSecs = [aDate timeIntervalSinceReferenceDate];

    NSTimeInterval dateStartInSecs = [start timeIntervalSinceReferenceDate];

    if (dateInSecs > (dateStartInSecs + extends) && dateInSecs < (dateStartInSecs + 2 * extends)) {

        return true;

    }else {

        return false;

    }

}


/**

 *  是不是上一周

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isLastWeek:(NSDate *) aDate

{

    NSDate *start;

    NSTimeInterval extends;

    NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];

    NSDate *today = [NSDate date];

    BOOL success = [cal rangeOfUnit:NSWeekdayCalendarUnit startDate:&start interval:&extends forDate:today];

    

    if (!success) {

        return false;

    }

    

    NSTimeInterval dateInSecs = [aDate timeIntervalSinceReferenceDate];

    NSTimeInterval dateStartInSecs = [start timeIntervalSinceReferenceDate];

    if (dateInSecs < dateStartInSecs && dateInSecs > (dateStartInSecs - extends)) {

        return true;

    }else {

        return false;

    }


}


/**

 *  是不是同一年

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isSameYearAsDate:(NSDate *) aDate andSecDate:(NSDate *) bDate

{

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

    [ds setDateFormat:@"yyyy"];

    NSString *bDateStr = [ds stringFromDate:bDate];

    NSString *aDateStr = [ds stringFromDate:aDate];

    

    if ([aDateStr isEqual:bDateStr]) {

        return true;

    }else {

        return false;

    }

}


/**

 *  是不是同一年

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isThisYear:(NSDate *) aDate

{

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

    [ds setDateFormat:@"yyyy"];

    NSString *currentDateStr = [ds stringFromDate:[NSDate date]];

    NSString *compareDateStr = [ds stringFromDate:aDate];

    

    if ([currentDateStr isEqual:compareDateStr]) {

        return true;

    }else {

        return false;

    }

}


/**

 *  是不是下一年

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isNextYear:(NSDate *) aDate

{

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

    [ds setDateFormat:@"yyyy"];

    NSString *currentDateStr = [ds stringFromDate:[NSDate date]];

    NSString *compareDateStr = [ds stringFromDate:aDate];

    

    NSInteger currentDateInt = [currentDateStr integerValue];

    NSInteger compareDateInt = [compareDateStr integerValue];

    if (compareDateInt - currentDateInt == 1) {

        return true;

    }else {

        return false;

    }

}


/**

 *  是不是上一年

 *

 *  @param aDate 日期

 *

 *  @return NSDate类型

 */

-(BOOL) isLastYear:(NSDate *) aDate

{

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

    [ds setDateFormat:@"yyyy"];

    NSString *currentDateStr = [ds stringFromDate:[NSDate date]];

    NSString *compareDateStr = [ds stringFromDate:aDate];

    

    NSInteger currentDateInt = [currentDateStr integerValue];

    NSInteger compareDateInt = [compareDateStr integerValue];

    if (currentDateInt - compareDateInt == 1) {

        return true;

    }else {

        return false;

    }

}


/**

 *  是不是比dDate

 *

 *  @param aDate 指定日期

 *

 *  @return NSDate类型

 */

-(BOOL) isEarlierThanDate:(NSDate *) aDate

{

    NSDate *date = [[NSDate alloc] init];

    if ([[date laterDate:aDate] isEqualToDate:aDate]) {

        return true;

    }else {

        return false;

    }

}


/**

 *  是不是比aData

 *

 *  @param aDate 指定日期

 *

 *  @return NSDate类型

 */

-(BOOL) isLaterThanDate:(NSDate *) aDate

{

    NSDate *date = [[NSDate alloc] init];

    if ([[date earlierDate:aDate] isEqualToDate:aDate]) {

        return false;

    }else {

        return true;

    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值