views:

45

answers:

2

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

I found what I needed here- http://stackoverflow.com/questions/1072848/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 some errors. Can you please help with this.

+1  A: 

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.

Cannonade
I will try this and let you know if I have any issues. Thanks!
nishantcm
@nishantcm no probs. Good luck.
Cannonade
Do I have to create these 2 separate files. NSDate+Helper.h and .m
nishantcm
@nishantcm Yep. Check out this post on extending interfaces http://it.toolbox.com/blogs/macsploitation/extending-classes-in-objectivec-with-categories-27447
Cannonade
@cannonade, I have made changes in my original question because i could not post code here. Plz help with that. Also is there a simple way of using this function or creating a simpler function for this use. Thanks for your help.
nishantcm
Hello, Dont bother answering that. I wrote a new method for this. Thanks for the help.
nishantcm
A: 

Hello,

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.

nishantcm