-1

I have implemented two textFields From and To in ViewController.When the user selects the textField he is able to select the date from the calendar. The selected Date is stored in the TextField as YYYY-MM-dd format.

And I have implemented a calculate button in ViewController.When the Two TextField are filled i need to do some work.

When the two textField are filled and the From TextField is greater , I want to display an alert when the From TextField date is Greater than To TextField.

How can i Compare it...?

Ravi Dhorajiya
  • 1,531
  • 3
  • 21
  • 26
remyr3my
  • 768
  • 1
  • 6
  • 19

4 Answers4

1

Perfect solution

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSString *textFieldDateString1 = @"2016-07-14";
NSString *textFieldDateString2 = @"2016-07-15";

NSDate *date1 = [dateFormatter dateFromString:textFieldDateString1];
NSDate *date2 = [dateFormatter dateFromString:textFieldDateString2];

if ([date1 compare:date2] == NSOrderedDescending) {
    NSLog(@"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
    NSLog(@"date1 is earlier than date2");
} else {
    NSLog(@"dates are the same");
}

Printed Result is

date1 is earlier than date2

Date Comparision

Compare Two dates

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • This *could* crash if the date string doesn't conform to the date format. So not so perfect after all. Also others posted the same answer before you did, so it's not clear what your answers adds. – Droppy Jul 14 '16 at 06:17
  • How can it crash?Try my answer.If you get any crash send me that report. – user3182143 Jul 14 '16 at 07:07
  • I gave here simple answer for user.If he gets the date from server,he has to get it to string first.After that he can convert to date and he can directly compare two dates. – user3182143 Jul 14 '16 at 07:09
  • If `textFieldDateString2` string is not in `yyyy-MM-dd` format then `date2` will be `nil` and that will crash in `[NSDate compare:]`. There is no code to check if the dates converted successfully. – Droppy Jul 14 '16 at 09:17
0

i think you are looking for this functionality..

    NSCalendar* currentCalendar = [NSCalendar currentCalendar];
    NSCalendarUnit unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
    NSDateComponents *differenceComponents = [currentCalendar components:unitFlags fromDate:postdate toDate:[NSDate date] options:0];
    NSInteger yearDifference = [differenceComponents year];
    NSInteger monthDifference = [differenceComponents month];
    NSInteger dayDifference = [differenceComponents day];
    NSInteger hourDifference = [differenceComponents hour];
    NSInteger minuteDifference = [differenceComponents minute];

i hope this will help you..

0
   NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setDateFormat: @"yyyy-MM-dd"];
    NSDate * dt1 ;
    NSDate * dt2 ;
    dt1 = [df dateFromString: txt1.text];
    dt2 = [df dateFromString: txt2.text];
    NSComparisonResult result = [dt1 compare: dt2];
    switch (result) {
        case NSOrderedAscending:
            NSLog(@"%@ is greater than %@", dt2, dt1);
            break;
        case NSOrderedDescending:
            NSLog(@"%@ is less %@", dt2, dt1);
            break;
        case NSOrderedSame:
            NSLog(@"%@ is equal to %@", dt2, dt1);
            break;
        default:
            NSLog(@"erorr dates %@, %@", dt2, dt1);
            break;
    }

Hope this will help you....

Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51
0

First you have to convert this two text field string date into NSDate object and and use compare: method of date to compare date.

NSString *dateString1 = @"2016-05-31"; //First text field value
NSString *dateString2 = @"2016-05-31"; //Second text field value
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSDate *date1=[dateFormatter dateFromString:dateString1];
NSDate *date2=[dateFormatter dateFromString:dateString2];

if ([date1 compare:date2] == NSOrderedDescending) {
    NSLog(@"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
    NSLog(@"date1 is earlier than date2");
} else {
    NSLog(@"dates are the same");
}
Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
  • @user3182143, why i am remove my code? this code was generated by me. – Jayesh Thanki Jul 13 '16 at 10:22
  • Ok Then wait see my answer. – user3182143 Jul 13 '16 at 10:24
  • It is also generated by me – user3182143 Jul 13 '16 at 10:25
  • @user3182143 It's strange you should ask others to remove their answer because someone has already provided the same code and yet you do exactly that 10 minutes later. The only issue with this answer is the use of `YYYY` instead of `yyyy`, but ultimately there is nothing wrong with it. – Droppy Jul 13 '16 at 10:31
  • In some what Nirav,Payal,Muruganadhm answeres are diffrent.Almost he has done what nirav has done below.See also I have done same kind of coding.But I did not post the answer first.After that only I posted here.Even little bit I have not copied the answer from Jayesh.If I copied I would have posted same answer. – user3182143 Jul 13 '16 at 10:39