开发者

How do I use this inBetweenDate message?

I am a newbie with iphone programming. I need some help with this code.

I found what I needed here- How to Check if an NSDate occurs between two other NSDates

But I have no idea how to use this block of code.

+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
    if ([date compare:beginDate] == NSOrderedAscending)
        return NO;

    if ([date compare:endDate] == NSOrderedDescending) 
        return NO;

    return YES;
}

I need help on how to use this function.

I created NSDATE+Helper.h and NSDATE+Helper.m

My NSDATE+Helper.h

#import <Foundation/Foundation.h>


@interface NSDATEHelper : NSDate {

}

@end

NSDATE+Helper.m

#import "NSDATE+Helper.h"


@implementation NSDATE (Helper)

+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
    if ([date compare:beginDate] == NSOrderedAscending)
        return NO;

    if ([date compare:endDate] == NSOrderedDescending) 
        return NO;

    return YES;
}

This is not working. I am getting开发者_JS百科 some errors. Can you please help with this.


This code takes an NSDate and tells you if it is between two other dates:

NSDate * queryDate = [NSDate date];
NSDate * startDate= [NSDate date];
NSDate * endDate = [NSDate date];

// is query date between startDate and endDate
if ([NSDate date:queryDate isBetweenDate:startDate andDate:endDate])

You are adding this static message by extending NSDate, so you will need to define something like:

@interface NSDate (Helper)
    + (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate;
@end

In NSDate+Helper.h and an NSDate+Helper.m with the implementation.


I have written a new method for this.

- (BOOL) isBetweenDate:(NSString *)str1 andDate:(NSString *)str2 {
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSDate *Date = [dateFormat dateFromString:datestr];
    NSDate * Date1 = [dateFormat dateFromString:str1];
    NSDate * Date2 = [dateFormat dateFromString:str2];
    NSComparisonResult comparison1 = [Date compare:Date1];
    NSComparisonResult comparison2 = [Date compare:Date2];
    if((comparison1 == NSOrderedSame || comparison1 == NSOrderedDescending) && (comparison2==NSOrderedSame || comparison2 == NSOrderedAscending))
    {
        return YES;
    }
    return NO;
}

I was finding it difficult to use the other way. Thank you for your help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜