I am using dateformatter to get days and time in the my application. But I am facing an issue when I change the language of the phone dateformatter returns me day and time of th
Try with this code:
NSDate *date=[NSDate date];
NSDateFormatter *objTimeFotmatter=[[NSDateFormatter alloc]init];
[objTimeFotmatter setDateFormat:@"HH:mm"];
[objTimeFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]
autorelease]];
NSDateFormatter *objDayFotmatter=[[NSDateFormatter alloc]init];
[objDayFotmatter setDateFormat:@"EEEE"];
[objDayFotmatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]
autorelease]];
NSString *objTime=[objTimeFotmatter stringFromDate:date];
NSString *objDay=[objDayFotmatter stringFromDate:date];
[objDayFotmatter release];
[objTimeFormatter release];
NSLog(@"objTime=%@",objTime);
NSLog(@"objDay=%@",objDay);
Swift
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let resultsDate = dateFormatter.string(from: date)
Add the following lines to the dateformatter.
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
In your case,
NSDateFormatter *objTimeFotmatter=[[NSDateFormatter alloc]init];
[objTimeFotmatter setDateFormat:@"HH:mm"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[objTimeFotmatter setLocale:usLocale];
Similarly for all dateformatters.