iOS开发资料收集区

iOS 分类收集

2016-09-08  本文已影响186人  屈涯

.h文件

#import

#define D_MINUTE60

#define D_HOUR3600

#define D_DAY86400

#define D_WEEK604800

#define D_YEAR31556926

@interfaceNSDate (Category)

/**

*

*当前时间转换为时间戳

*

*/

+ (NSString*)getNowTimeStamp;

/**

*

*时间戳转换为时间

*

*/

+ (NSString*)getNowTime: (NSString*)stamp;

/**

*

*得到当前时间,yyyy-MM-dd HH:mm:ss

*

*/

+(NSString*)getNowChinaTime;

- (NSString*)timeIntervalDescription;//距离当前的时间间隔描述

- (NSString*)minuteDescription;/*精确到分钟的日期描述*/

- (NSString*)formattedTime;

- (NSString*)formattedDateDescription;//格式化日期描述

- (double)timeIntervalSince1970InMilliSecond;

+ (NSDate*)dateWithTimeIntervalInMilliSecondSince1970:(double)timeIntervalInMilliSecond;

+ (NSString*)formattedTimeFromTimeInterval:(longlong)time;

// Relative dates from the current date

+ (NSDate*) dateTomorrow;

+ (NSDate*) dateYesterday;

+ (NSDate*) dateWithDaysFromNow: (NSInteger) days;

+ (NSDate*) dateWithDaysBeforeNow: (NSInteger) days;

+ (NSDate*) dateWithHoursFromNow: (NSInteger) dHours;

+ (NSDate*) dateWithHoursBeforeNow: (NSInteger) dHours;

+ (NSDate*) dateWithMinutesFromNow: (NSInteger) dMinutes;

+ (NSDate*) dateWithMinutesBeforeNow: (NSInteger) dMinutes;

// Comparing dates

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

- (BOOL) isToday;

- (BOOL) isTomorrow;

- (BOOL) isYesterday;

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

- (BOOL) isThisWeek;

- (BOOL) isNextWeek;

- (BOOL) isLastWeek;

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

- (BOOL) isThisMonth;

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

- (BOOL) isThisYear;

- (BOOL) isNextYear;

- (BOOL) isLastYear;

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

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

- (BOOL) isInFuture;

- (BOOL) isInPast;

// Date roles

- (BOOL) isTypicallyWorkday;

- (BOOL) isTypicallyWeekend;

// Adjusting dates

- (NSDate*) dateByAddingDays: (NSInteger) dDays;

- (NSDate*) dateBySubtractingDays: (NSInteger) dDays;

- (NSDate*) dateByAddingHours: (NSInteger) dHours;

- (NSDate*) dateBySubtractingHours: (NSInteger) dHours;

- (NSDate*) dateByAddingMinutes: (NSInteger) dMinutes;

- (NSDate*) dateBySubtractingMinutes: (NSInteger) dMinutes;

- (NSDate*) dateAtStartOfDay;

// Retrieving intervals

- (NSInteger) minutesAfterDate: (NSDate*) aDate;

- (NSInteger) minutesBeforeDate: (NSDate*) aDate;

- (NSInteger) hoursAfterDate: (NSDate*) aDate;

- (NSInteger) hoursBeforeDate: (NSDate*) aDate;

- (NSInteger) daysAfterDate: (NSDate*) aDate;

- (NSInteger) daysBeforeDate: (NSDate*) aDate;

- (NSInteger)distanceInDaysToDate:(NSDate*)anotherDate;

// Decomposing dates

@property(readonly)NSIntegernearestHour;

@property(readonly)NSIntegerhour;

@property(readonly)NSIntegerminute;

@property(readonly)NSIntegerseconds;

@property(readonly)NSIntegerday;

@property(readonly)NSIntegermonth;

@property(readonly)NSIntegerweek;

@property(readonly)NSIntegerweekday;

@property(readonly)NSIntegernthWeekday;// e.g. 2nd Tuesday of the month == 2

@property(readonly)NSIntegeryear;

@end

.m文件

#import"NSDate+Category.h"

#import"NSDateFormatter+Category.h"

#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)

#define CURRENT_CALENDAR [NSCalendar currentCalendar]

@implementationNSDate (Category)

+(NSString*)getNowTimeStamp {

NSDateFormatter*formatter = [[NSDateFormatteralloc]init];

[formattersetDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate*date = [NSDatedate];

NSTimeZone*zone = [NSTimeZonesystemTimeZone];

NSIntegerinterval = [zonesecondsFromGMTForDate:date];

NSDate*localeDate = [datedateByAddingTimeInterval: interval];

NSString*timeSp = [NSStringstringWithFormat:@"%ld",(long)[localeDatetimeIntervalSince1970]];

NSLog(@"当前时间的时间戳:%@",timeSp);

returntimeSp;

}

+ (NSString*)getNowTime: (NSString*)stamp{

NSTimeIntervaltime=[stampdoubleValue] ;//因为时差问题要加8小时== 28800 sec

NSDate*detaildate=[NSDatedateWithTimeIntervalSince1970:time];

NSLog(@"date:%@",[detaildatedescription]);

NSString*currentDateStr = [detaildatedescription];

return[currentDateStrsubstringToIndex:19];

}

+(NSString*)getNowChinaTime {

NSDate*date = [NSDatedate];

//如果这里转换了就是格林威治时间

//时区转换,取得系统时区,取得格林威治时间差秒

//NSTimeIntervaltimeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];

////NSLog(@"%f",timeZoneOffset/60.0/60.0);

//

//date = [date dateByAddingTimeInterval:timeZoneOffset];

//格式化日期时间

NSDateFormatter*dateformatter=[[NSDateFormatteralloc]init];

[dateformattersetDateFormat:@"yyyyMMdd"];

NSString* dateStr=[dateformatterstringFromDate:date];

NSLog(@"当前时间:%@",dateStr);

returndateStr;

}

/*距离当前的时间间隔描述*/

- (NSString*)timeIntervalDescription

{

NSTimeIntervaltimeInterval = -[selftimeIntervalSinceNow];

if(timeInterval <60) {

returnNSLocalizedString(@"NSDateCategory.text1",@"");

}elseif(timeInterval <3600) {

return[NSStringstringWithFormat:NSLocalizedString(@"NSDateCategory.text2",@""), timeInterval /60];

}elseif(timeInterval <86400) {

return[NSStringstringWithFormat:NSLocalizedString(@"NSDateCategory.text3",@""), timeInterval /3600];

}elseif(timeInterval <2592000) {//30天内

return[NSStringstringWithFormat:NSLocalizedString(@"NSDateCategory.text4",@""), timeInterval /86400];

}elseif(timeInterval <31536000) {//30天至1年内

NSDateFormatter*dateFormatter = [NSDateFormatterdateFormatterWithFormat:NSLocalizedString(@"NSDateCategory.text5",@"")];

return[dateFormatterstringFromDate:self];

}else{

return[NSStringstringWithFormat:NSLocalizedString(@"NSDateCategory.text6",@""), timeInterval /31536000];

}

}

/*精确到分钟的日期描述*/

- (NSString*)minuteDescription

{

NSDateFormatter*dateFormatter = [NSDateFormatterdateFormatterWithFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString*theDay = [dateFormatterstringFromDate:self];//日期的年月日

NSString*currentDay = [dateFormatterstringFromDate:[NSDatedate]];//当前年月日

if([theDayisEqualToString:currentDay]) {//当天

[dateFormattersetDateFormat:@"ah:mm"];

return[dateFormatterstringFromDate:self];

}elseif([[dateFormatterdateFromString:currentDay]timeIntervalSinceDate:[dateFormatterdateFromString:theDay]] ==86400) {//昨天

[dateFormattersetDateFormat:@"ah:mm"];

return[NSStringstringWithFormat:NSLocalizedString(@"NSDateCategory.text7", @'"'), [dateFormatterstringFromDate:self]];

}elseif([[dateFormatterdateFromString:currentDay]timeIntervalSinceDate:[dateFormatterdateFromString:theDay]] <86400*7) {//间隔一周内

[dateFormattersetDateFormat:@"EEEE ah:mm"];

return[dateFormatterstringFromDate:self];

}else{//以前

[dateFormattersetDateFormat:@"yyyy-MM-dd ah:mm"];

return[dateFormatterstringFromDate:self];

}

}

/*标准时间日期描述*/

-(NSString*)formattedTime{

NSDateFormatter* formatter = [[NSDateFormatteralloc]init];

[formattersetDateFormat:@"YYYY-MM-dd"];

NSString* dateNow = [formatterstringFromDate:[NSDatedate]];

NSDateComponents*components = [[NSDateComponentsalloc]init];

[componentssetDay:[[dateNowsubstringWithRange:NSMakeRange(8,2)]intValue]];

[componentssetMonth:[[dateNowsubstringWithRange:NSMakeRange(5,2)]intValue]];

[componentssetYear:[[dateNowsubstringWithRange:NSMakeRange(0,4)]intValue]];

NSCalendar*gregorian = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDate*date = [gregoriandateFromComponents:components];//今天0点时间

NSIntegerhour = [selfhoursAfterDate:date];

NSDateFormatter*dateFormatter =nil;

NSString*ret =@"";

//hasAMPM==TURE为12小时制,否则为24小时制

NSString*formatStringForHours = [NSDateFormatterdateFormatFromTemplate:@"j"options:0locale:[NSLocalecurrentLocale]];

NSRangecontainsA = [formatStringForHoursrangeOfString:@"a"];

BOOLhasAMPM = containsA.location!=NSNotFound;

if(!hasAMPM) {//24小时制

if(hour <=24&& hour >=0) {

dateFormatter = [NSDateFormatterdateFormatterWithFormat:@"HH:mm"];

}elseif(hour <0&& hour >= -24) {

dateFormatter = [NSDateFormatterdateFormatterWithFormat:NSLocalizedString(@"NSDateCategory.text8",@"")];

}else{

dateFormatter = [NSDateFormatterdateFormatterWithFormat:@"yyyy-MM-dd"];

}

}else{

if(hour >=0&& hour <=6) {

dateFormatter = [NSDateFormatterdateFormatterWithFormat:NSLocalizedString(@"NSDateCategory.text9",@"")];

}elseif(hour >6&& hour <=11) {

dateFormatter = [NSDateFormatterdateFormatterWithFormat:NSLocalizedString(@"NSDateCategory.text10",@"")];

}elseif(hour >11&& hour <=17) {

dateFormatter = [NSDateFormatterdateFormatterWithFormat:NSLocalizedString(@"NSDateCategory.text11",@"")];

}elseif(hour >17&& hour <=24) {

dateFormatter = [NSDateFormatterdateFormatterWithFormat:NSLocalizedString(@"NSDateCategory.text12",@"")];

}elseif(hour <0&& hour >= -24){

dateFormatter = [NSDateFormatterdateFormatterWithFormat:NSLocalizedString(@"NSDateCategory.text13",@"")];

}else{

dateFormatter = [NSDateFormatterdateFormatterWithFormat:@"yyyy-MM-dd"];

}

}

ret = [dateFormatterstringFromDate:self];

returnret;

}

/*格式化日期描述*/

- (NSString*)formattedDateDescription

{

NSDateFormatter*dateFormatter = [[NSDateFormatteralloc]init];

[dateFormattersetDateFormat:@"yyyy-MM-dd"];

NSString*theDay = [dateFormatterstringFromDate:self];//日期的年月日

NSString*currentDay = [dateFormatterstringFromDate:[NSDatedate]];//当前年月日

NSIntegertimeInterval = -[selftimeIntervalSinceNow];

if(timeInterval <60) {

returnNSLocalizedString(@"NSDateCategory.text1",@"");

}elseif(timeInterval <3600) {//1小时内

return[NSStringstringWithFormat:NSLocalizedString(@"NSDateCategory.text2",@""), timeInterval /60];

}elseif(timeInterval <21600) {//6小时内

return[NSStringstringWithFormat:NSLocalizedString(@"NSDateCategory.text3",@""), timeInterval /3600];

}elseif([theDayisEqualToString:currentDay]) {//当天

[dateFormattersetDateFormat:@"HH:mm"];

return[NSStringstringWithFormat:NSLocalizedString(@"NSDateCategory.text14",@""), [dateFormatterstringFromDate:self]];

}elseif([[dateFormatterdateFromString:currentDay]timeIntervalSinceDate:[dateFormatterdateFromString:theDay]] ==86400) {//昨天

[dateFormattersetDateFormat:@"HH:mm"];

return[NSStringstringWithFormat:NSLocalizedString(@"NSDateCategory.text7",@""), [dateFormatterstringFromDate:self]];

}else{//以前

[dateFormattersetDateFormat:@"yyyy-MM-dd HH:mm"];

return[dateFormatterstringFromDate:self];

}

}

- (double)timeIntervalSince1970InMilliSecond {

doubleret;

ret = [selftimeIntervalSince1970] *1000;

returnret;

}

+ (NSDate*)dateWithTimeIntervalInMilliSecondSince1970:(double)timeIntervalInMilliSecond {

NSDate*ret =nil;

doubletimeInterval = timeIntervalInMilliSecond;

// judge if the argument is in secconds(for former data structure).

if(timeIntervalInMilliSecond >140000000000) {

timeInterval = timeIntervalInMilliSecond /1000;

}

ret = [NSDatedateWithTimeIntervalSince1970:timeInterval];

returnret;

}

+ (NSString*)formattedTimeFromTimeInterval:(longlong)time{

return[[NSDatedateWithTimeIntervalInMilliSecondSince1970:time]formattedTime];

}

#pragma mark Relative Dates

+ (NSDate*) dateWithDaysFromNow: (NSInteger) days

{

// Thanks, Jim Morrison

return[[NSDatedate]dateByAddingDays:days];

}

+ (NSDate*) dateWithDaysBeforeNow: (NSInteger) days

{

// Thanks, Jim Morrison

return[[NSDatedate]dateBySubtractingDays:days];

}

+ (NSDate*) dateTomorrow

{

return[NSDatedateWithDaysFromNow:1];

}

+ (NSDate*) dateYesterday

{

return[NSDatedateWithDaysBeforeNow:1];

}

+ (NSDate*) dateWithHoursFromNow: (NSInteger) dHours

{

NSTimeIntervalaTimeInterval = [[NSDatedate]timeIntervalSinceReferenceDate] +D_HOUR* dHours;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

returnnewDate;

}

+ (NSDate*) dateWithHoursBeforeNow: (NSInteger) dHours

{

NSTimeIntervalaTimeInterval = [[NSDatedate]timeIntervalSinceReferenceDate] -D_HOUR* dHours;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

returnnewDate;

}

+ (NSDate*) dateWithMinutesFromNow: (NSInteger) dMinutes

{

NSTimeIntervalaTimeInterval = [[NSDatedate]timeIntervalSinceReferenceDate] +D_MINUTE* dMinutes;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

returnnewDate;

}

+ (NSDate*) dateWithMinutesBeforeNow: (NSInteger) dMinutes

{

NSTimeIntervalaTimeInterval = [[NSDatedate]timeIntervalSinceReferenceDate] -D_MINUTE* dMinutes;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

returnnewDate;

}

#pragma mark Comparing Dates

- (BOOL) isEqualToDateIgnoringTime: (NSDate*) aDate

{

NSDateComponents*components1 = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

NSDateComponents*components2 = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:aDate];

return((components1.year== components2.year) &&

(components1.month== components2.month) &&

(components1.day== components2.day));

}

- (BOOL) isToday

{

return[selfisEqualToDateIgnoringTime:[NSDatedate]];

}

- (BOOL) isTomorrow

{

return[selfisEqualToDateIgnoringTime:[NSDatedateTomorrow]];

}

- (BOOL) isYesterday

{

return[selfisEqualToDateIgnoringTime:[NSDatedateYesterday]];

}

// This hard codes the assumption that a week is 7 days

- (BOOL) isSameWeekAsDate: (NSDate*) aDate

{

NSDateComponents*components1 = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

NSDateComponents*components2 = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:aDate];

// Must be same week. 12/31 and 1/1 will both be week "1" if they are in the same week

if(components1.week!= components2.week)returnNO;

// Must have a time interval under 1 week. Thanks @aclark

return(abs([selftimeIntervalSinceDate:aDate])

}

- (BOOL) isThisWeek

{

return[selfisSameWeekAsDate:[NSDatedate]];

}

- (BOOL) isNextWeek

{

NSTimeIntervalaTimeInterval = [[NSDatedate]timeIntervalSinceReferenceDate] +D_WEEK;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

return[selfisSameWeekAsDate:newDate];

}

- (BOOL) isLastWeek

{

NSTimeIntervalaTimeInterval = [[NSDatedate]timeIntervalSinceReferenceDate] -D_WEEK;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

return[selfisSameWeekAsDate:newDate];

}

// Thanks, mspasov

- (BOOL) isSameMonthAsDate: (NSDate*) aDate

{

NSDateComponents*components1 = [CURRENT_CALENDARcomponents:NSYearCalendarUnit|NSMonthCalendarUnitfromDate:self];

NSDateComponents*components2 = [CURRENT_CALENDARcomponents:NSYearCalendarUnit|NSMonthCalendarUnitfromDate:aDate];

return((components1.month== components2.month) &&

(components1.year== components2.year));

}

- (BOOL) isThisMonth

{

return[selfisSameMonthAsDate:[NSDatedate]];

}

- (BOOL) isSameYearAsDate: (NSDate*) aDate

{

NSDateComponents*components1 = [CURRENT_CALENDARcomponents:NSYearCalendarUnitfromDate:self];

NSDateComponents*components2 = [CURRENT_CALENDARcomponents:NSYearCalendarUnitfromDate:aDate];

return(components1.year== components2.year);

}

- (BOOL) isThisYear

{

// Thanks, baspellis

return[selfisSameYearAsDate:[NSDatedate]];

}

- (BOOL) isNextYear

{

NSDateComponents*components1 = [CURRENT_CALENDARcomponents:NSYearCalendarUnitfromDate:self];

NSDateComponents*components2 = [CURRENT_CALENDARcomponents:NSYearCalendarUnitfromDate:[NSDatedate]];

return(components1.year== (components2.year+1));

}

- (BOOL) isLastYear

{

NSDateComponents*components1 = [CURRENT_CALENDARcomponents:NSYearCalendarUnitfromDate:self];

NSDateComponents*components2 = [CURRENT_CALENDARcomponents:NSYearCalendarUnitfromDate:[NSDatedate]];

return(components1.year== (components2.year-1));

}

- (BOOL) isEarlierThanDate: (NSDate*) aDate

{

return([selfcompare:aDate] ==NSOrderedAscending);

}

- (BOOL) isLaterThanDate: (NSDate*) aDate

{

return([selfcompare:aDate] ==NSOrderedDescending);

}

// Thanks, markrickert

- (BOOL) isInFuture

{

return([selfisLaterThanDate:[NSDatedate]]);

}

// Thanks, markrickert

- (BOOL) isInPast

{

return([selfisEarlierThanDate:[NSDatedate]]);

}

#pragma mark Roles

- (BOOL) isTypicallyWeekend

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:NSWeekdayCalendarUnitfromDate:self];

if((components.weekday==1) ||

(components.weekday==7))

returnYES;

returnNO;

}

- (BOOL) isTypicallyWorkday

{

return![selfisTypicallyWeekend];

}

#pragma mark Adjusting Dates

- (NSDate*) dateByAddingDays: (NSInteger) dDays

{

NSTimeIntervalaTimeInterval = [selftimeIntervalSinceReferenceDate] +D_DAY* dDays;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

returnnewDate;

}

- (NSDate*) dateBySubtractingDays: (NSInteger) dDays

{

return[selfdateByAddingDays: (dDays * -1)];

}

- (NSDate*) dateByAddingHours: (NSInteger) dHours

{

NSTimeIntervalaTimeInterval = [selftimeIntervalSinceReferenceDate] +D_HOUR* dHours;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

returnnewDate;

}

- (NSDate*) dateBySubtractingHours: (NSInteger) dHours

{

return[selfdateByAddingHours: (dHours * -1)];

}

- (NSDate*) dateByAddingMinutes: (NSInteger) dMinutes

{

NSTimeIntervalaTimeInterval = [selftimeIntervalSinceReferenceDate] +D_MINUTE* dMinutes;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

returnnewDate;

}

- (NSDate*) dateBySubtractingMinutes: (NSInteger) dMinutes

{

return[selfdateByAddingMinutes: (dMinutes * -1)];

}

- (NSDate*) dateAtStartOfDay

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

components.hour=0;

components.minute=0;

components.second=0;

return[CURRENT_CALENDARdateFromComponents:components];

}

- (NSDateComponents*) componentsWithOffsetFromDate: (NSDate*) aDate

{

NSDateComponents*dTime = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:aDatetoDate:selfoptions:0];

returndTime;

}

#pragma mark Retrieving Intervals

- (NSInteger) minutesAfterDate: (NSDate*) aDate

{

NSTimeIntervalti = [selftimeIntervalSinceDate:aDate];

return(NSInteger) (ti /D_MINUTE);

}

- (NSInteger) minutesBeforeDate: (NSDate*) aDate

{

NSTimeIntervalti = [aDatetimeIntervalSinceDate:self];

return(NSInteger) (ti /D_MINUTE);

}

- (NSInteger) hoursAfterDate: (NSDate*) aDate

{

NSTimeIntervalti = [selftimeIntervalSinceDate:aDate];

return(NSInteger) (ti /D_HOUR);

}

- (NSInteger) hoursBeforeDate: (NSDate*) aDate

{

NSTimeIntervalti = [aDatetimeIntervalSinceDate:self];

return(NSInteger) (ti /D_HOUR);

}

- (NSInteger) daysAfterDate: (NSDate*) aDate

{

NSTimeIntervalti = [selftimeIntervalSinceDate:aDate];

return(NSInteger) (ti /D_DAY);

}

- (NSInteger) daysBeforeDate: (NSDate*) aDate

{

NSTimeIntervalti = [aDatetimeIntervalSinceDate:self];

return(NSInteger) (ti /D_DAY);

}

// Thanks, dmitrydims

// I have not yet thoroughly tested this

- (NSInteger)distanceInDaysToDate:(NSDate*)anotherDate

{

NSCalendar*gregorianCalendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents*components = [gregorianCalendarcomponents:NSDayCalendarUnitfromDate:selftoDate:anotherDateoptions:0];

returncomponents.day;

}

#pragma mark Decomposing Dates

- (NSInteger) nearestHour

{

NSTimeIntervalaTimeInterval = [[NSDatedate]timeIntervalSinceReferenceDate] +D_MINUTE*30;

NSDate*newDate = [NSDatedateWithTimeIntervalSinceReferenceDate:aTimeInterval];

NSDateComponents*components = [CURRENT_CALENDARcomponents:NSHourCalendarUnitfromDate:newDate];

returncomponents.hour;

}

- (NSInteger) hour

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

returncomponents.hour;

}

- (NSInteger) minute

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

returncomponents.minute;

}

- (NSInteger) seconds

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

returncomponents.second;

}

- (NSInteger) day

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

returncomponents.day;

}

- (NSInteger) month

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

returncomponents.month;

}

- (NSInteger) week

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

returncomponents.week;

}

- (NSInteger) weekday

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

returncomponents.weekday;

}

- (NSInteger) nthWeekday// e.g. 2nd Tuesday of the month is 2

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

returncomponents.weekdayOrdinal;

}

- (NSInteger) year

{

NSDateComponents*components = [CURRENT_CALENDARcomponents:DATE_COMPONENTSfromDate:self];

returncomponents.year;

}

@end

上一篇下一篇

猜你喜欢

热点阅读