在目标c中将NSString转换为NSDate

时间:2018-01-31 06:19:02

标签: objective-c nsstring nsdate

我得到的NSString为@“27-Jan-2018 00:00”。我只想将此字符串转换为NSDate类。请提示一些实现此方法的方法。提前谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个......

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

答案 1 :(得分:-3)

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

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