如何在iphone中将nsstring转换为nsdate

如何在iphone中将nsstring转换为nsdate,iphone,objective-c,Iphone,Objective C,我想将nsstring转换为nsdate。我创建了一个全局变量,保存从日期选择器中选择的日期,然后使用datefromstring函数将此变量转换为nsdate。但是字符串没有转换为日期。可能是什么问题 TTimePickercontroller: This is the class in which i have the code for the picker -(NSString *)convertDueDateFormat{ NSDateFormatter *dateFormat

我想将nsstring转换为nsdate。我创建了一个全局变量,保存从日期选择器中选择的日期,然后使用datefromstring函数将此变量转换为nsdate。但是字符串没有转换为日期。可能是什么问题

TTimePickercontroller:
This is the class in which i have the code for the picker

-(NSString *)convertDueDateFormat{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    [dateFormatter setFormatterBehavior: NSDateFormatterBehavior10_4];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    dateString = [dateFormatter stringFromDate:datePicker.date];

    return dateString;
//dateString is an nsstring variable that has been declared globally.
}

TAddAlarmController.
this is another class in which i want to convert the dateString varibale into nssdate
-(IBAction)save{

    timepicker = [[TTimePickerController alloc]init];
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"hh:mm"];
    d = [df dateFromString:dateString];
    NSLog(@"%@", d);
    //Here i am using the dateFromString function and converting the string variable dateString into nsdate variable d .in dateString variable value is getting passed but in d the value is not getting passed. 


}
Please help me in solving the problem.Thanks
}


但是,当我在控制台中将其转换为d时,它显示:1970-01-01 05:24:00+0000nikunj,但当我将此dateStrimg转换为nsdate时,它没有得到正确转换。如果我选择1.00 pm,它将在我的nsdate变量中显示7.30 pm,请稍等10分钟,我将实现您的降级此链接,这将帮助您了解为什么需要nsdate&无论您在何处展示?请告诉我,这样我就能解决你的问题了
-(NSString *)convertDueDateFormat{

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm"];
 dateString = [dateFormatter stringFromDate:datePicker.date];
   ////some code    
Try like this,
Try this code this will help you because I run this code successfully and return time.

UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];  
pickerView.datePickerMode = UIDatePickerModeDate;  
pickerView.hidden = NO;  
pickerView.date = [NSDate date];
[self.view addSubview:pickerView];
NSString *dateString;   
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm"];
dateString = [dateFormatter stringFromDate:pickerView.date];
NSLog(@"String into Date:--->%@",dateString);