Reputation: 10139
I was using NSDateformatter for parsing a date. It was working fine til I updated to iOS 7.0.4. But since the update I am getting a nil value,
This is the date I am trying to parse
11/20/2013 3:30:05 PM
Below is the code for the same
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = @"MM/dd/yyyy hh:mm:ss a";
[dateFormatter1 setLocale:[NSLocale currentLocale]];
NSDate *date = [dateFormatter1 dateFromString:[items objectAtIndex:2]];
But I am getting nil as the date.
How can I make this work in IOS 7.0.4?
Upvotes: 3
Views: 936
Reputation: 1218
If it is displaying a different hour you may need to set the default timezone (this happens only after ios7)
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
it seems that the default timezone is the one the device has, so if you no specify the default timezone you might get strange results. Prior to ios7 it was always GMT.
Upvotes: 0
Reputation: 69499
Your date is localized, but you are using the device its locale. But the AM/PM may be different in other locales.
-(void)dateTest {
NSString *inputDate = @"11/20/2013 3:30:05 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = @"MM/dd/yyyy hh:mm:ss a";
[dateFormatter1 setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *date = [dateFormatter1 dateFromString:inputDate];
NSLog(@"date: %@", date); //OUTPUT date: 2013-11-20 15:30:05 +0000
}
Upvotes: 4
Reputation: 25459
Can you post what string is stored in [items objectAtIndex:2]? I try it and it works fine for me:
-(void)dateTest
{
NSString *inputDate = @"11/20/2013 3:30:05 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = @"MM/dd/yyyy hh:mm:ss a";
[dateFormatter1 setLocale:[NSLocale currentLocale]];
NSDate *date = [dateFormatter1 dateFromString:inputDate];
NSLog(@"date: %@", date); //OUTPUT date: 2013-11-20 15:30:05 +0000
}
Upvotes: 2