1

Possible Duplicate:
How to compare two NSDate objects in objective C

1---> I'm new to Objective-C and stuck at a point. The concept is of type booking slots/reservation, which means cannot book a slot at previous time and previous date. I have to compare today's date with a particular date may be less than today's date or greater than today's date.

For getting current date I have done this :

NSDate *date = [NSDate date];
 NSDateFormatter *formatter = nil;
 formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];

I have a date which is to be compared with todays date in UItextField which is also in yyyy-MM-dd format.

Now I have to compare today's date with the textField date.But I'm not getting it ..

if(textfielddate<currentdate)
{
   NSLog(@"You cannot book a slot at past date);
}

but here it is not getting compared..

2---> I need to compare a time in 24hr format with currenttime which is also in 24hr format..

I have a particular time in a string variable 12:00,11:00,15:00 etc .Now I need to compare this with the current time in 24 hr format .

if([time <= currenttime] && [textFielddate==currentdate])
{
    NSLog(@"You cannot book a slot at past time ");
}

How can I do it?

Community
  • 1
  • 1
07405
  • 193
  • 4
  • 14
  • see my answer from this link http://stackoverflow.com/questions/13229142/how-to-compare-current-date-to-previous-date-in-iphone/13229277#13229277 – Paras Joshi Nov 06 '12 at 05:15

2 Answers2

1

BEst way is to use NSCompariosnResult

[date1 compare:date2];

then check whether ascending or descending or equal.

Regards Deepak

DD_
  • 7,230
  • 11
  • 38
  • 59
  • But I need to have both the dates in yyyy-MM-dd format only .When I write this :NSDate *dtee = [NSDate date]; NSDateFormatter *formatter = nil; formatter=[[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSString *s = [NSString stringWithFormat:@"%@",[displaydate text]]; NSDateFormatter *dateFormatter100 = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter100 setDateFormat:@"yyyy-MM-dd"]; NSDate *date100 = [dateFormatter100 dateFromString: s]; NSLog(@"%@",date100); NSComparisonResult result; result = [dtee compare:date100]; – 07405 Nov 06 '12 at 05:51
  • if(result==NSOrderedAscending) NSLog(@"todayDate is less"); else if(result==NSOrderedDescending) NSLog(@"yourCompDate is less"); else NSLog(@"Both dates are same"); here dtee is todays date and date100 is also todays date but still getting result "as yourCompDate is less"..It should be both dates are same ..what elz should I need to modify .? – 07405 Nov 06 '12 at 05:52
  • al that you need is to compare the date with current date right? – DD_ Nov 06 '12 at 06:06
  • you are not in right path, i will correct it wait – DD_ Nov 06 '12 at 06:13
  • new code here NSDate *dtee = [NSDate date]; NSDateFormatter *dateFormatter100 = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter100 setDateFormat:@"yyyy-MM-dd"]; NSString *s = [NSString stringWithFormat:@"%@",[displaydate text]]; NSDate *date100 = [dateFormatter100 dateFromString: s]; NSComparisonResult result = [dtee compare:date100]; if(result==NSOrderedAscending) NSLog(@"todayDate is less"); else if(result==NSOrderedDescending) NSLog(@"yourCompDate is less"); elseNSLog(@"Both dates are same"); – DD_ Nov 06 '12 at 06:21
1

For Checking whether the date entered in the UITextField is previous or not. You can create a new date using the values entered in the textfield. Suppose the value entered is 06-Nov-2012 then from that textfield you have to get the day, month and year components in integers programmatically. Get the hours, minutes and seconds also in integers for the time for which you need to check that it is a previous time or not. Create a date with desired time using:

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:iDay];
    [comps setMonth:iMonth];
    [comps setYear:iYear];
    [comps setHour:iHour];
    [comps setMinute:iMinutes];
    [comps setSecond:iSeconds];

    NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateToCheck = [gregorian dateFromComponents:comps];
    [comps release];

Once date is created you can check whether the date is previous one or not using :

    double timestamp = [dateToCheck timeIntervalSinceDate:[NSDate date]];
    if(timestamp < 0)
    {
        // The date is previous one
    }
Chirag Bhutani
  • 229
  • 1
  • 8