vignesh vignesh - 4 months ago 9
iOS Question

how to compare two dates using its month and year in iOS

how to compare two dates using its month and year in iOS

*date1=@"21/07/2016";
date2=@"20/08/2016";
NSComparisonResult result;
result = [date1 compare:date2];*

It compares only date and show output date1 is less
now i want to compare this dates using its month and year also

Answer

You need to convert your string to date and then compare it will give you proper result.

Try below code.

NSString *strTimeIn=@"21/07/2016";
NSString *strTimeOut=@"20/08/2016";

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"dd/MM/yyyy"];
NSDate *dtOne=[format dateFromString:strTimeIn];
NSDate *dtTwo=[format dateFromString:strTimeOut];

NSComparisonResult result;
result = [dtOne compare:dtTwo];
Comments