iOS 時(shí)間處理匯總

關(guān)于時(shí)間的全局變量和宏,下面方法用到砌创。

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


static CGFloat const D_MINUTE = 60;
static CGFloat const D_HOUR = 3600;
static CGFloat const D_DAY = 86400;
static CGFloat const D_WEEK = 604800;
static CGFloat const D_YEAR = 31556926;

獲取某個(gè)時(shí)間的年虏缸、月、日嫩实、小時(shí)刽辙、分鐘、秒

- (NSInteger)year
{
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
    return [dateComponents year];
}

- (NSInteger)month
{
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
    return [dateComponents month];
}

- (NSInteger)day
{
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
    return [dateComponents day];
}

- (NSInteger)hour
{
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
    return [dateComponents hour];
}

- (NSInteger)minute
{
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
    return [dateComponents minute];
}

- (NSInteger)second
{
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
    return [dateComponents second];
}
/**
 *  N'天'后日期
 */
NSDate *nextDate = [NSDate dateWithTimeInterval:D_DAY *  days sinceDate:self.currentDate];

/**
 *  N'天'前日期
 */
NSDate *nextDate = [NSDate dateWithTimeInterval: - D_DAY *  days sinceDate:self.currentDate];

全部在NSDate分類里面實(shí)現(xiàn)

一甲献、月末最后一天

/**
 月末最后一天

 @param date 當(dāng)前時(shí)間對(duì)象
 @return 月末日期
 */
+ (NSDate *)lastDayOfMonthWithDate:(NSDate *)date
{
    double interval = 0;
    NSDate *firstDate = nil;
    NSDate *lastDate  = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    BOOL OK = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:date];
    
    if (OK) {
        lastDate = [firstDate dateByAddingTimeInterval:interval - 1];
    }
    
    return lastDate;
}

二宰缤、月初第一天

/**
 月初第一天

 @param date 當(dāng)前時(shí)間對(duì)象
 @return 月初日期
 */
+ (NSDate *)firstDayOfMonthWithDate:(NSDate *)date
{
    double interval = 0;
    NSDate *firstDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:date];
    
    return firstDate;
}

三、本周周一

/**
 本周周一 一周時(shí)間起點(diǎn)按周一算起(周一晃洒、周二慨灭、周三、周四球及、周五氧骤、周六、周日)
 
 @param date 當(dāng)前時(shí)間對(duì)象
 @return 本周周一
 */
+ (NSDate *)mondayOfWeekWithDate:(NSDate *)date {
    
    NSTimeInterval interval;
    NSDate *mondayDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2]; //Override locale to make week start on Monday
    [calendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:& mondayDate interval:&interval forDate:date];
    return mondayDate;
}

四吃引、本周周日

/**
 本周周日 一周時(shí)間起點(diǎn)按周日算起(周日筹陵、周一、周二镊尺、周三朦佩、周四、周五鹅心、周六)
 
 @param date 當(dāng)前時(shí)間對(duì)象
 @return 本周周日
 */
+ (NSDate *)sundayOfWeekWithDate:(NSDate *)date {
    NSTimeInterval interval;
    NSDate *sundayDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:& sundayDate interval:&interval forDate:date];
    return sundayDate;
}

五吕粗、N'月'前或者后時(shí)間

//某個(gè)時(shí)間前或者后N個(gè)月
+ (NSDate *)getForwardOrLaterDateFromDate:(NSDate *)currentDate month:(NSInteger)month {
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *tempDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    return tempDate;
}

六纺荧、N'天'前或者后時(shí)間

//N天后時(shí)間
- (NSDate *) dateByAddingDays: (NSInteger) dDays
{
    NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_DAY * dDays;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return newDate;
}
//N'天'前時(shí)間
- (NSDate *) dateBySubtractingDays: (NSInteger) dDays
{
    return [self dateByAddingDays: (dDays * -1)];
}

七旭愧、N'分鐘'前或者后時(shí)間

//N分鐘后時(shí)間
- (NSDate *) dateByAddingMinutes: (NSInteger) dMinutes
{
    NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return newDate;
}
//N分鐘前時(shí)間
- (NSDate *) dateBySubtractingMinutes: (NSInteger) dMinutes
{
    return [self dateByAddingMinutes: (dMinutes * -1)];
}

八、N'小時(shí)'前或者后時(shí)間

//N小時(shí)后時(shí)間
- (NSDate *) dateByAddingHours: (NSInteger) dHours
{
    NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_HOUR * dHours;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return newDate;
}
//N小時(shí)前時(shí)間
- (NSDate *) dateBySubtractingHours: (NSInteger) dHours
{
    return [self dateByAddingHours: (dHours * -1)];
}

九宙暇、昨天

/**
 *  昨天
 *
 *  @return 昨天日期
 */
+ (NSDate *) dateYesterday
{
    return [[NSDate date] dateBySubtractingDays:1];
}

十输枯、明天

/**
 *  明天
 *
 *  @return 明天日期
 */
+ (NSDate *) dateTomorrow
{
    rreturn [[NSDate date] dateByAddingDays:1];
}

十一、是否為今天占贫、是否為明天桃熄、是否為昨天

//是否為今天
- (BOOL) isToday
{
    return [self isEqualToDateIgnoringTime:[NSDate date]];
}
//是否為明天
- (BOOL) isTomorrow
{
    return [self isEqualToDateIgnoringTime:[NSDate dateTomorrow]];
}
//是否為昨天
- (BOOL) isYesterday
{
    return [self isEqualToDateIgnoringTime:[NSDate dateYesterday]];
}

- (BOOL) isEqualToDateIgnoringTime: (NSDate *) aDate
{
    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:DATE_COMPONENTS fromDate:self];
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:DATE_COMPONENTS fromDate:aDate];
    return ((components1.year == components2.year) &&
            (components1.month == components2.month) &&
            (components1.day == components2.day));
}

十二、關(guān)于周 是否屬于同一周型奥、 是否本周瞳收、是否上一周碉京、是否為下周

//是否屬于同一周
- (BOOL) isSameWeekWithDate: (NSDate *) date
{
    NSDateComponents *components1 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    NSDateComponents *components2 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate: date];
    
    if (components1.weekOfYear != components2.weekOfYear) return NO;
    
    return (fabs([self timeIntervalSinceDate:aDate]) < D_WEEK);
}
//是否為本周
- (BOOL) isThisWeek
{
    return [self isSameWeekWithDate:[NSDate date]];
}
//是否為下一周
- (BOOL) isNextWeek
{
    NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_WEEK;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return [self isSameWeekWithDate:newDate];
}
//是否為上一周
- (BOOL) isLastWeek
{
    NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] - D_WEEK;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return [self isSameWeekWithDate:newDate];
}

十三、關(guān)于月的 是否同一個(gè)月螟深、是否本月

//是否屬于同一個(gè)月
- (BOOL) isSameMonthWithDate: (NSDate *) date
{
    NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self];
    NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate: date];
    return ((components1.month == components2.month) &&
            (components1.year == components2.year));
}
//是否為本月
- (BOOL) isThisMonth
{
    return [self isSameMonthWithDate:[NSDate date]];
}

十四谐宙、關(guān)于年的 是否屬于同一年、是否為今年界弧、是否上一年凡蜻、是否下一年

//是否屬于同一年
- (BOOL) isSameYearWithDate: (NSDate *) date
{
    NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
    NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate: date];
    return (components1.year == components2.year);
}
//是否為今年
- (BOOL) isThisYear
{
    return [self isSameYearWithDate:[NSDate date]];
}
//是否為下一年
- (BOOL) isNextYear
{
    NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
    NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:[NSDate date]];
    
    return (components1.year == (components2.year + 1));
}
//是否為前年
- (BOOL) isLastYear
{
    NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
    NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:[NSDate date]];
    
    return (components1.year == (components2.year - 1));
}

十五、時(shí)間大小比較

//大于當(dāng)前時(shí)間
- (BOOL) isEarlierThanDate: (NSDate *) date
{
    return ([self compare: date] == NSOrderedAscending);
}
//小于當(dāng)前時(shí)間
- (BOOL) isLaterThanDate: (NSDate *) date
{
    return ([self compare: date] == NSOrderedDescending);
}

二垢箕、設(shè)置時(shí)間戳 實(shí)現(xiàn)可以在NSString 分類中 或者工具類中

+ (NSString *)getDateWithTargetDate:(NSDate *)targetDate formatter:(NSString *)formatter {
    if (!targetDate) {
        return @"";
    }
    if (!formatter) {
        formatter = @"yyyy-MM-dd HH:mm";
    }
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    dateformatter.dateFormat = formatter;
    NSString *  tempString=[dateformatter stringFromDate:targetDate];
    return tempString;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末划栓,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子条获,更是在濱河造成了極大的恐慌忠荞,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件帅掘,死亡現(xiàn)場(chǎng)離奇詭異钻洒,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)锄开,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門素标,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人萍悴,你說(shuō)我怎么就攤上這事头遭。” “怎么了癣诱?”我有些...
    開封第一講書人閱讀 152,998評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵计维,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我撕予,道長(zhǎng)鲫惶,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,323評(píng)論 1 279
  • 正文 為了忘掉前任实抡,我火速辦了婚禮欠母,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吆寨。我一直安慰自己赏淌,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,355評(píng)論 5 374
  • 文/花漫 我一把揭開白布啄清。 她就那樣靜靜地躺著六水,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上掷贾,一...
    開封第一講書人閱讀 49,079評(píng)論 1 285
  • 那天睛榄,我揣著相機(jī)與錄音,去河邊找鬼想帅。 笑死懈费,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的博脑。 我是一名探鬼主播憎乙,決...
    沈念sama閱讀 38,389評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼叉趣!你這毒婦竟也來(lái)了泞边?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,019評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤疗杉,失蹤者是張志新(化名)和其女友劉穎阵谚,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體烟具,經(jīng)...
    沈念sama閱讀 43,519評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡梢什,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,971評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了朝聋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嗡午。...
    茶點(diǎn)故事閱讀 38,100評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖冀痕,靈堂內(nèi)的尸體忽然破棺而出荔睹,到底是詐尸還是另有隱情,我是刑警寧澤言蛇,帶...
    沈念sama閱讀 33,738評(píng)論 4 324
  • 正文 年R本政府宣布僻他,位于F島的核電站,受9級(jí)特大地震影響腊尚,放射性物質(zhì)發(fā)生泄漏吨拗。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,293評(píng)論 3 307
  • 文/蒙蒙 一婿斥、第九天 我趴在偏房一處隱蔽的房頂上張望劝篷。 院中可真熱鬧,春花似錦受扳、人聲如沸携龟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春华望,著一層夾襖步出監(jiān)牢的瞬間蕊蝗,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工赖舟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蓬戚,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,547評(píng)論 2 354
  • 正文 我出身青樓宾抓,卻偏偏與公主長(zhǎng)得像子漩,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子石洗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,834評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • iOS應(yīng)用程序最酷的特性之一就是應(yīng)用將其自身”綁定”到一個(gè)自定義 URL scheme 上幢泼,該 scheme 用于...
    HJXu閱讀 281評(píng)論 0 1
  • 從3月11日開始備戰(zhàn)小升初,這里的備戰(zhàn)是指帶著兒子參加私立中學(xué)的入學(xué)考試讲衫。小升初兒子四年級(jí)時(shí)就想到擇校的問題了缕棵,原...
    王沐妍閱讀 82評(píng)論 0 1
  • 任何時(shí)候都要有向上的心 很長(zhǎng)一段時(shí)間我都做了一些無(wú)用的事,每天循環(huán)重復(fù)涉兽,做事情提不起半點(diǎn)精神招驴。 看過(guò)一句話“說(shuō)了很...
    喵鮮閱讀 357評(píng)論 0 1