0

I wanted to compare a user input date string to current date. My requirement is that,the date entered by the user should always be a future date. For this, I am converting the string value to date and comparing to [NSDate date]. But dateFromString is always returning a date prior to user entered date. Here is my code:

 NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM/dd/yy"];
    NSDate *myDate=[dateFormatter dateFromString:@"04/11/2014"];
    NSComparisonResult result = [(NSDate*)[NSDate date] compare:myDate];
    NSLog(@"myDate is %@",myDate);
    NSLog(@"currrent date is %@",[NSDate date]);
    if(result==NSOrderedDescending)
    {
        NSLog(@"the date entered is past date");
    }

And following is the output:

myDate is 2014-04-10 18:30:00 +0000
currrent date is 2014-04-11 09:23:28 +0000
the date entered is past date

How do I compare the string date to current date. (My timezone is IST- GMT + 5:30).

triandicAnt
  • 1,328
  • 2
  • 15
  • 40
  • make sure to use the correct timezones and also note that NSLog of a date object will always print in GMT... – Volker Apr 11 '14 at 09:35

1 Answers1

0

Don't worry about the output. You should notice that these dates are correct if you add your timezone and the comparison works fine in this case. Probably you need to compare ignoring time. This you can see here: https://stackoverflow.com/a/1854919/2317866

Community
  • 1
  • 1
Mike
  • 481
  • 10
  • 24