-4

i follow stackOverflow answer and Write a code but i Don't know What happen.it won't work. plz,any one can solve it for me. i use fooling code....

        NSMutableArray *movieDate;
    NSArray *temp1 = [_dicUpcomingMovie objectForKey:@"results"];
    for (NSDictionary *temp2 in temp1)
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

        NSString *datestring = [temp2 valueForKey:@"release_date"];
        //NSDate *myDate = [[NSDate alloc]init];
        [formatter setDateFormat:@"dd MMM,YYYY"];
        NSDate *date = [formatter dateFromString:datestring];
        NSLog(@"%@",date);
        [movieDate addObject:date];
        NSLog(@"%@",movieDate);
    }

so this is it. the above code was not work myDate object still nil

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Suraj Lokhande
  • 323
  • 1
  • 2
  • 11
  • What is your date format, please print the Array or dictionary data? – Iyyappan Ravi Jun 13 '16 at 11:38
  • I guess that `datestring` and the dateFormat (`dd MMM, YYY`) don't match (It the best guess, but you didn't give a sample). Also, avoid doing the alloc/init of the `NSDateFormatter` object each time in the loop. First it's quite consuming, and also you use the same each time. – Larme Jun 13 '16 at 11:39
  • Print the date fromat – Nirav D Jun 13 '16 at 11:46
  • As temp1 is an NSArray, so for loop it should be for (NSString *temp2 in temp1) – Minkle Garg Jun 13 '16 at 12:01
  • what is the date string release_date? Please be specific – Bittoo Jun 13 '16 at 12:23
  • Possible duplicate of [How can I convert string date to NSDate?](http://stackoverflow.com/questions/24777496/how-can-i-convert-string-date-to-nsdate) – Sneha Jun 13 '16 at 12:30
  • 1
    Date formatters only work if the dateFormat matches the strings you are trying to convert. Any mismatch and the conversion fails. Post sample date strings that fail or we can't help you. – Duncan C Jun 13 '16 at 13:45

3 Answers3

0

I believe that datestring doesn't match the format you specified. See NSDateFormatter for a complete example.

guidev
  • 2,695
  • 2
  • 23
  • 44
0
    NSMutableArray *movieDate=[[NSMutableArray alloc]init];;
   NSArray *temp1 = [_dicUpcomingMovie objectForKey:@"results"];
  NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    for (int i=0 ;i<temp1.count;i++)
  {


    NSString *datestring = [[temp1   objectatindex:i]valueForKey:@"release_date"];

  [serverFormatter setTimeZone:[NSTimeZone localTimeZone]];
  [serverFormatter setDateFormat:@"dd,MMM,YYYY"];
   NSDate *date = [serverFormatter dateFromString:dateString1];

    [movieDate addObject:date];

}
   NSLog(@"%@",movieDate);
Dinesh Gurrapu
  • 158
  • 1
  • 1
  • 11
-1

Try this,

NSMutableArray *movieDate;
    NSArray *temp1 = [_dicUpcomingMovie objectForKey:@"results"];
    for (NSDictionary *temp2 in temp1)
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

        NSString *datestring = [temp2 valueForKey:@"release_date"];
        //NSDate *myDate = [[NSDate alloc]init];
        [formatter setDateFormat:@"dd MM,YYYY"];
        NSDate *date = [formatter dateFromString:datestring];
        NSLog(@"%@",date);
        [movieDate addObject:date];
        NSLog(@"%@",movieDate);
    }
Sunny
  • 821
  • 6
  • 17