开发者

How to convert nsstring into nsdate in iphone

i want to convert an nsstring into nsdate .i have created a global variable where i saving the date picked from the datepicker and then converting this variable to nsdate us the datefromstring function.But the string is not getting converted to date.What may be the problem.

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开发者_如何学Go 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜