Note that there are some explanatory texts on larger screens.

plurals
  1. POFormatting a date from string returns NULL
    text
    copied!<p>I have two different strings, one for the date and the other one for the time. I have combined the two and I want to get an NSDate from it.</p> <pre><code>NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSString *format = [NSDateFormatter dateFormatFromTemplate:@"yyyy-MM-dd" options:0 locale:[NSLocale currentLocale]]; [formatter setDateFormat:format]; NSString *date = [formatter stringFromDate:temp]; NSLog(@"%@",date); //This shows 10/11/2013 NSDateFormatter *formatterTime = [[NSDateFormatter alloc] init]; NSString *formatTime = [NSDateFormatter dateFormatFromTemplate:@"j:mm" options:0 locale:[NSLocale currentLocale]]; [formatterTime setDateFormat:formatTime]; NSDate *tTime = [formatterTime dateFromString:[self.arrayOrari objectAtIndex:a]]; NSString *time = [formatterTime stringFromDate:tTime]; NSLog(@"%@",time); //This shows 6:58 PM NSString *dateTime = [NSString stringWithFormat:@"%@ %@",date,time]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"yyyy-MM-dd j:mm" options:0 locale:[NSLocale currentLocale]]; [dateFormatter setDateFormat:formatString]; myMed.orario = [dateFormatter dateFromString:dateTime]; NSLog(@"%@",myMed.orario); //This shows NULL </code></pre> <p>I've been trying since about four hours, but I'm not able to solve this problem. Any idea?</p> <p>UPDATE: I've tried this, as suggested:</p> <pre><code>NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSString *format = [NSDateFormatter dateFormatFromTemplate:@"yyyy-MM-dd" options:0 locale:[NSLocale currentLocale]]; [formatter setDateFormat:format]; NSString *date = [formatter stringFromDate:temp]; NSLog(@"%@",date); //This shows 10/11/2013 NSDateFormatter *formatterTime = [[NSDateFormatter alloc] init]; NSString *formatTime = [NSDateFormatter dateFormatFromTemplate:@"HH:mm" options:0 locale:[NSLocale currentLocale]]; [formatterTime setDateFormat:formatTime]; NSDate *tTime = [formatterTime dateFromString:[self.arrayOrari objectAtIndex:a]]; NSString *time = [formatterTime stringFromDate:tTime]; NSLog(@"%@",time); //This shows 18:58 NSString *dateTime = [NSString stringWithFormat:@"%@ %@",date,time]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"yyyy-MM-dd HH:mm" options:0 locale:[NSLocale currentLocale]]; [dateFormatter setDateFormat:formatString]; myMed.orario = [dateFormatter dateFromString:dateTime]; NSLog(@"%@",myMed.orario); //This shows NULL </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload