iOS判斷兩個日期是否為同一周

星期一到星期天為一周,創(chuàng)建NSDate擴展類
NSDate+ Helper.h文件代碼如下:

#import <Foundation/Foundation.h>

#define D_MINUTE  60
#define D_HOUR    3600
#define D_DAY     86400
#define D_WEEK    604800

@interface NSDate (Helper)

@property (readonly) NSInteger weekday;         // 系統(tǒng)默認weekday:星期日對應weekday=1
@property (readonly) NSInteger customWeekday;   // 中國weekday:星期一對應weekday=1

+ (NSCalendar *)currentCalendar;
// 判斷兩個日期是否為同一周的方法
- (BOOL)isSameWeekAsDate:(NSDate *)anotherDate;

@end

NSDate+Helper.m文件代碼如下:

#import "NSDate+Helper.h"

static const unsigned componentFlags = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitQuarter);

@implementation NSDate (Helper)

#pragma mark - Public Method
+ (NSCalendar *) currentCalendar
{
    static NSCalendar *sharedCalendar = nil;
    if (!sharedCalendar)
        sharedCalendar = [NSCalendar autoupdatingCurrentCalendar];
    return sharedCalendar;
}

- (BOOL) isSameWeekAsDate:(NSDate *)otherDate
{
    BOOL result = NO;
    
    NSTimeInterval startInterval = [self timeIntervalAtStartOfDaySince1970];
    NSTimeInterval endInterval = [self timeIntervalAtEndOfDaySince1970];
    NSTimeInterval otherInterval = [otherDate timeIntervalSince1970];
    NSUInteger weekday = [self customWeekday];
    startInterval = startInterval - (weekday-1) * D_DAY;
    endInterval = endInterval + (7-weekday) * D_DAY;
    if (otherInterval >= startInterval && otherInterval <= endInterval) {
        result = YES;
    }
    
    return result;
}

#pragma mark - Private Method
- (NSDate *) dateAtStartOfDay
{
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    components.hour = 0;
    components.minute = 0;
    components.second = 0;
    return [[NSDate currentCalendar] dateFromComponents:components];
}

- (NSDate *) dateAtEndOfDay
{
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    components.hour = 23;
    components.minute = 59;
    components.second = 59;
    return [[NSDate currentCalendar] dateFromComponents:components];
}

- (NSTimeInterval)timeIntervalAtStartOfDaySince1970
{
    return [[self dateAtStartOfDay] timeIntervalSince1970];
}

- (NSTimeInterval)timeIntervalAtEndOfDaySince1970
{
    return [[self dateAtEndOfDay] timeIntervalSince1970];
}

#pragma mark - getter or setter
- (NSInteger) weekday
{
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    return components.weekday;
}

- (NSInteger)customWeekday
{
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    return components.weekday==1 ? 7 : --components.weekday;
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容