iOS 分类收集

.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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容