Search code examples
iosobjective-cios6nsdateios6.1

date returned from dateFromString method


Please help me solve the problem.

(The values for objValidation.aRange.MinimumValue, objValidation.aRange.MaximumValue come from web services)

(I am getting wrong dates in firstDate, secondDate.)

NSDateFormatter *df  = [[NSDateFormatter alloc] init];
                [df setDateFormat:@"MM/dd/yyyy"];
                [df setTimeZone:[NSTimeZone localTimeZone]];
                NSDate *myDate = [df dateFromString:inputString];
                [df setDateFormat:@"MM/dd/yyyy"];
                NSLog(@"objValidation.aRange.MinimumValue : %@,objValidation.aRange.MaximumValue :%@, inputString : %@",objValidation.aRange.MinimumValue,objValidation.aRange.MaximumValue,inputString);
                NSDate *firstDate = [df dateFromString:objValidation.aRange.MinimumValue];
                NSDate *secondDate = [df dateFromString:objValidation.aRange.MaximumValue];
                NSLog(@"myDate : %@,firstDate :%@, secondDate : %@",myDate,firstDate,secondDate);
                [df release];

Output:

objValidation.aRange.MinimumValue : 8/20/2013
objValidation.aRange.MaximumValue :12/31/9999
inputString : 08/22/2013 
myDate : 2013-08-21 18:30:00 +0000
firstDate :2013-08-19 18:30:00 +0000
secondDate : 1999-12-30 18:30:00 +0000 

Solution

  • The date retuned is correct, but you do not take in account the timezone.

    All NSDate object do not have an timezone. So when you parse the date the system timezone will be used.