I am getting a NSString as @"27-Jan-2018 00:00".i just want to convert this string to NSDate class.Kindly suggest some methods to achieve this.Thanks in advance!
Asked
Active
Viewed 104 times
-3
-
see this.. https://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-back-again – Nirav Kotecha Jan 31 '18 at 06:25
2 Answers
0
Try this...
NSString *dateString = @"27-Jan-2018 00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(@"%@",dateFromString);

Nirav Kotecha
- 2,493
- 1
- 12
- 26
-
1Assuming the strings will always use English month names, you also need to set the date formatter's locale to `en_US_POSIX`. – rmaddy Jan 31 '18 at 07:05
-
-3
Please, check it below code. Maybe its work for you and your needs.
-(void)DateChange
{
NSString *Input_Date =@"31-Jan-18 04:25 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yy hh:mm a"];
NSDate * Format_date = [dateFormatter dateFromString:Input_Date];
[dateFormatter setDateFormat:@"dd-MMM-yy hh:mm a"];
NSString *Change_date = [dateFormatter stringFromDate:Format_date];
NSLog(@"Final Change Date :- %@",Change_date);
}
My Output is :-
Final Change Date :- 31-Jan-18 04:25 AM

ManiVasanth
- 86
- 6