在 objective c 中将 NSString 转换为 NSDate

Convert NSString to NSDate in objective c

我得到一个 NSString 作为@"27-Jan-2018 00:00"。我只想将这个字符串转换为 NSDate class.Kindly 建议一些方法来实现 this.Thanks前进!

试试这个...

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);

请检查下面的代码。也许它适合您和您的需求。

 -(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