// // NSDate+YYAdd.m // YYKit // // Created by ibireme on 13/4/11. // Copyright (c) 2015 ibireme. // // This source code is licensed under the MIT-style license found in the // LICENSE file in the root directory of this source tree. // #import "NSDate+YYAdd.h" #import "YYKitMacro.h" #import YYSYNTH_DUMMY_CLASS(NSDate_YYAdd) @implementation NSDate (YYAdd) - (NSInteger)year { return [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:self] year]; } - (NSInteger)month { return [[[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:self] month]; } - (NSInteger)day { return [[[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:self] day]; } - (NSInteger)hour { return [[[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:self] hour]; } - (NSInteger)minute { return [[[NSCalendar currentCalendar] components:NSCalendarUnitMinute fromDate:self] minute]; } - (NSInteger)second { return [[[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:self] second]; } - (NSInteger)nanosecond { return [[[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:self] nanosecond]; } - (NSInteger)weekday { return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:self] weekday]; } - (NSInteger)weekdayOrdinal { return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekdayOrdinal fromDate:self] weekdayOrdinal]; } - (NSInteger)weekOfMonth { return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekOfMonth fromDate:self] weekOfMonth]; } - (NSInteger)weekOfYear { return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekOfYear fromDate:self] weekOfYear]; } - (NSInteger)yearForWeekOfYear { return [[[NSCalendar currentCalendar] components:NSCalendarUnitYearForWeekOfYear fromDate:self] yearForWeekOfYear]; } - (NSInteger)quarter { return [[[NSCalendar currentCalendar] components:NSCalendarUnitQuarter fromDate:self] quarter]; } - (BOOL)isLeapMonth { return [[[NSCalendar currentCalendar] components:NSCalendarUnitQuarter fromDate:self] isLeapMonth]; } - (BOOL)isLeapYear { NSUInteger year = self.year; return ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))); } - (BOOL)isToday { if (fabs(self.timeIntervalSinceNow) >= 60 * 60 * 24) return NO; return [NSDate new].day == self.day; } - (BOOL)isYesterday { NSDate *added = [self dateByAddingDays:1]; return [added isToday]; } - (NSDate *)dateByAddingYears:(NSInteger)years { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setYear:years]; return [calendar dateByAddingComponents:components toDate:self options:0]; } - (NSDate *)dateByAddingMonths:(NSInteger)months { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setMonth:months]; return [calendar dateByAddingComponents:components toDate:self options:0]; } - (NSDate *)dateByAddingWeeks:(NSInteger)weeks { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setWeekOfYear:weeks]; return [calendar dateByAddingComponents:components toDate:self options:0]; } - (NSDate *)dateByAddingDays:(NSInteger)days { NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + 86400 * days; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } - (NSDate *)dateByAddingHours:(NSInteger)hours { NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + 3600 * hours; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } - (NSDate *)dateByAddingMinutes:(NSInteger)minutes { NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + 60 * minutes; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } - (NSDate *)dateByAddingSeconds:(NSInteger)seconds { NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + seconds; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } - (NSString *)stringWithFormat:(NSString *)format { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:format]; [formatter setLocale:[NSLocale currentLocale]]; return [formatter stringFromDate:self]; } - (NSString *)stringWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:format]; if (timeZone) [formatter setTimeZone:timeZone]; if (locale) [formatter setLocale:locale]; return [formatter stringFromDate:self]; } - (NSString *)stringWithISOFormat { static NSDateFormatter *formatter = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ formatter = [[NSDateFormatter alloc] init]; formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ"; }); return [formatter stringFromDate:self]; } + (NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:format]; return [formatter dateFromString:dateString]; } + (NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:format]; if (timeZone) [formatter setTimeZone:timeZone]; if (locale) [formatter setLocale:locale]; return [formatter dateFromString:dateString]; } + (NSDate *)dateWithISOFormatString:(NSString *)dateString { static NSDateFormatter *formatter = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ formatter = [[NSDateFormatter alloc] init]; formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ"; }); return [formatter dateFromString:dateString]; } @end