#import <Foundation/Foundation.h>
@interface DateHelper : NSObject
/************************************************
*获取日期(date_)对用的元素
*/
+ (int)second:(NSDate *)date_;
+ (int)minute:(NSDate *)date_;
+ (int)hour:(NSDate *)date_;
+ (int)day:(NSDate *)date_;
+ (int)month:(NSDate *)date_;
+ (int)year:(NSDate *)date_;
/************************************************/
/************************************************
*判断date_是否和当前日期在指定的范围之内
*/
+ (BOOL)isDateThisWeek:(NSDate *)date_;
+ (BOOL)isDateThisMonth:(NSDate *)date_;
+ (BOOL)isDateThisYear:(NSDate *)date_;
/************************************************/
/************************************************
*判断两个时间是否在指定的范围之内
*/
+ (BOOL)twoDateIsSameYear:(NSDate *)fistDate_
second:(NSDate *)secondDate_;
+ (BOOL)twoDateIsSameMonth:(NSDate *)fistDate_
second:(NSDate *)secondDate_;
+ (BOOL)twoDateIsSameDay:(NSDate *)fistDate_
second:(NSDate *)secondDate_;
/************************************************/
/* 获取指定日期所在月的天数 */
+ (int)numberDaysInMonthOfDate:(NSDate *)date_;
+ (NSDate *)dateByAddingComponents:(NSDate *)date_
offsetComponents:(NSDateComponents *)offsetComponents_;
/************************************************
*获取指定日期所在的月对应的月开始时间和月结束时间
*/
+ (NSDate *)startDateInMonthOfDate:(NSDate *)date_;
+ (NSDate *)endDateInMonthOfDate:(NSDate *)date_;
/************************************************/
- (BOOL)isDateThisWeek:(NSDate *)date;
@end
.m
/.m
//
// DateHelper.m
// TestDate
//代码原始地址:http://blog.csdn.net/diyagoanyhacker
// Created by 禚来强 email:ZhuoLaiQiang@Gmail.com QQ:1287976580 电话:18671682672 on 12-3-7.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "DateHelper.h"
@interface DateHelper()
+ (int)ordinality:(NSDate *)date_ ordinalitySign:(NSCalendarUnit)ordinalitySign_;
@end
@implementation DateHelper
#pragma mark - 外部接口
+ (int)second:(NSDate *)date_
{
int ordinality = [DateHelperordinality: date_ordinalitySign:NSSecondCalendarUnit];
return ordinality;
}
+ (int)minute:(NSDate *)date_
{
int ordinality = [DateHelperordinality: date_ordinalitySign:NSMinuteCalendarUnit];
return ordinality;
}
+ (int)hour:(NSDate *)date_
{
int ordinality = [DateHelperordinality: date_ordinalitySign:NSHourCalendarUnit];
return ordinality;
}
+ (int)day:(NSDate *)date_
{
int ordinality = [DateHelperordinality: date_ordinalitySign:NSDayCalendarUnit];
return ordinality;
}
+ (int)month:(NSDate *)date_
{
int ordinality = [DateHelperordinality: date_ordinalitySign:NSMonthCalendarUnit];
return ordinality;
}
+ (int)year:(NSDate *)date_
{
int ordinality = [DateHelperordinality: date_ordinalitySign:NSYearCalendarUnit];
return ordinality;
}
/* 判断date_是否在当前星期 */
+ (BOOL)isDateThisWeek:(NSDate *)date_
{
NSDate *start;
NSTimeInterval extends;
NSCalendar *cal=[NSCalendarautoupdatingCurrentCalendar];
NSDate *today=[NSDatedate];
BOOL success= [calrangeOfUnit:NSWeekCalendarUnitstartDate:&start
interval: &extendsforDate:today];
if(!success)returnNO;
NSTimeInterval dateInSecs = [date_timeIntervalSinceReferenceDate];
NSTimeInterval dayStartInSecs= [start timeIntervalSinceReferenceDate];
if(dateInSecs > dayStartInSecs && dateInSecs < (dayStartInSecs+extends))
{
return YES;
}
else
{
return NO;
}
}
+ (BOOL)isDateThisMonth:(NSDate *)date_
{
NSDate *start;
NSTimeInterval extends;
NSCalendar *cal=[NSCalendarautoupdatingCurrentCalendar];
NSDate *today=[NSDatedate];
BOOL success= [calrangeOfUnit:NSMonthCalendarUnit
startDate: &start
interval: &extendsforDate:today];
if(!success) returnNO;
NSTimeInterval dateInSecs = [date_timeIntervalSinceReferenceDate];
NSTimeInterval dayStartInSecs= [start timeIntervalSinceReferenceDate];
if(dateInSecs > dayStartInSecs && dateInSecs < (dayStartInSecs+extends))
{
return YES;
}
else
{
return NO;
}
}
+ (BOOL)isDateThisYear:(NSDate *)date_
{
NSDate *start;
NSTimeInterval extends;
NSCalendar *cal=[NSCalendarautoupdatingCurrentCalendar];
NSDate *today=[NSDatedate];
BOOL success= [calrangeOfUnit:NSYearCalendarUnit
startDate: &start
interval: &extendsforDate:today];
if(!success)returnNO;
NSTimeInterval dateInSecs = [date_timeIntervalSinceReferenceDate];
NSTimeInterval dayStartInSecs= [start timeIntervalSinceReferenceDate];
if(dateInSecs > dayStartInSecs && dateInSecs < (dayStartInSecs+extends))
{
return YES;
}
else
{
return NO;
}
}
+ (BOOL)twoDateIsSameYear:(NSDate *)fistDate_
second:(NSDate *)secondDate_
{
NSCalendar *calendar = [NSCalendarcurrentCalendar];
int unit =NSYearCalendarUnit;
NSDateComponents *fistComponets = [calendarcomponents: unitfromDate: fistDate_];
NSDateComponents *secondComponets = [calendarcomponents: unit
fromDate: secondDate_];
if ([fistComponets year] == [secondComponetsyear])
{
return YES;
}
return NO;
}
+ (BOOL)twoDateIsSameMonth:(NSDate *)fistDate_
second:(NSDate *)secondDate_
{
NSCalendar *calendar = [NSCalendarcurrentCalendar];
int unit =NSMonthCalendarUnit |NSYearCalendarUnit;
NSDateComponents *fistComponets = [calendarcomponents: unitfromDate: fistDate_];
NSDateComponents *secondComponets = [calendarcomponents: unit
fromDate: secondDate_];
if ([fistComponets month] == [secondComponetsmonth]
&& [fistComponets year] == [secondComponetsyear])
{
return YES;
}
return NO;
}
+ (BOOL)twoDateIsSameDay:(NSDate *)fistDate_
second:(NSDate *)secondDate_
{
NSCalendar *calendar = [NSCalendarcurrentCalendar];
int unit =NSMonthCalendarUnit |NSYearCalendarUnit |NSDayCalendarUnit;
NSDateComponents *fistComponets = [calendarcomponents: unit
fromDate: fistDate_];
NSDateComponents *secondComponets = [calendarcomponents: unit
fromDate: secondDate_];
if ([fistComponets day] == [secondComponetsday]
&& [fistComponets month] == [secondComponetsmonth]
&& [fistComponets year] == [secondComponetsyear])
{
return YES;
}
return NO;
}
+ (int)numberDaysInMonthOfDate:(NSDate *)date_
{
NSCalendar *calender = [NSCalendarcurrentCalendar];
NSRange range = [calenderrangeOfUnit:NSDayCalendarUnit
inUnit: NSMonthCalendarUnit
forDate: date_];
return range.length;
}
+ (NSDate *)dateByAddingComponents:(NSDate *)date_
offsetComponents:(NSDateComponents *)offsetComponents_
{
NSCalendar *gregorian = [[[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
NSDate *endOfWorldWar3 = [gregorian dateByAddingComponents: offsetComponents_
toDate: date_
options: 0];
return endOfWorldWar3;
}
+ (NSDate *)startDateInMonthOfDate:(NSDate *)date_
{
double interval = 0;
NSDate *beginningOfMonth = nil;
NSCalendar *gregorian = [NSCalendarcurrentCalendar];
BOOL ok = [gregorianrangeOfUnit:NSMonthCalendarUnit
startDate: &beginningOfMonth
interval: &interval
forDate: date_];
if (ok)
{
return beginningOfMonth;
}
else
{
return nil;
}
}
+ (NSDate *)endDateInMonthOfDate:(NSDate *)date_
{
double interval = 0;
NSDate *beginningOfMonth = nil;
NSCalendar *gregorian = [NSCalendarcurrentCalendar];
BOOL ok = [gregorianrangeOfUnit:NSMonthCalendarUnit
startDate: &beginningOfMonth
interval: &interval
forDate: date_];
if (ok)
{
NSDate *endDate = [beginningOfMonthaddTimeInterval: interval];
return endDate;
}
else
{
return nil;
}
}
- (BOOL)isDateThisWeek:(NSDate *)date
{
NSDate *start;
NSTimeInterval extends;
NSCalendar *cal=[NSCalendarautoupdatingCurrentCalendar];
NSDate *today=[NSDatedate];
BOOL success= [calrangeOfUnit:NSWeekCalendarUnitstartDate:&start
interval: &extendsforDate:today];
if(!success)returnNO;
NSTimeInterval dateInSecs = [datetimeIntervalSinceReferenceDate];
NSTimeInterval dayStartInSecs= [start timeIntervalSinceReferenceDate];
if(dateInSecs > dayStartInSecs && dateInSecs < (dayStartInSecs+extends)){
return YES;
}
else {
return NO;
}
}
#pragma mark - 私有方法
+ (int)ordinality:(NSDate *)date_ ordinalitySign:(NSCalendarUnit)ordinalitySign_
{
int ordinality = -1;
if (ordinalitySign_ < NSEraCalendarUnit || ordinalitySign_ > NSWeekdayOrdinalCalendarUnit)
{
return ordinality;
}
NSCalendar *calendar = [NSCalendarcurrentCalendar];
NSDateComponents *components = [calendarcomponents: ordinalitySign_fromDate: date_];
switch (ordinalitySign_)
{
caseNSSecondCalendarUnit:
{
ordinality = [components second];
break;
}
caseNSMinuteCalendarUnit:
{
ordinality = [components minute];
break;
}
caseNSHourCalendarUnit:
{
ordinality = [components hour];
break;
}
caseNSDayCalendarUnit:
{
ordinality = [components day];
break;
}
caseNSMonthCalendarUnit:
{
ordinality = [components month];
break;
}
caseNSYearCalendarUnit:
{
ordinality = [components year];
break;
}
default:
break;
}
return ordinality;
}
@end