-3

I am trying to convert string to date but it returns null.

  NSString *offer_publishdate*=@"15/10/2013";
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyyMMdd"];
        NSDate *date = [dateFormat dateFromString:offer_publishdate];
          NSLog(@"date:%@",date);
royhowie
  • 11,075
  • 14
  • 50
  • 67
user2702179
  • 187
  • 2
  • 13
  • 3
    please read about it in the iOS Developer Library before posting such questions [link](https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html) – falsecrypt Oct 18 '13 at 07:28
  • Why in the world do people try to use date formats that don't match the format of the date???? – Hot Licks Oct 18 '13 at 07:38
  • First i have given the right format,later i found something like this – user2702179 Oct 18 '13 at 09:40

3 Answers3

1

Please select the exact date formatting

 NSString *offer_publishdate*=@"15/10/2013";
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSDate *date = [dateFormat dateFromString:offer_publishdate];
      NSLog(@"date:%@",date);
Shashank Kulshrestha
  • 1,556
  • 17
  • 31
0

Try this way....

 NSString *offer_publishdate*=@"15/10/2013";
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"dd/MM/yyyy"];
        NSDate *date = [dateFormat dateFromString:offer_publishdate];
user1673099
  • 3,293
  • 7
  • 26
  • 57
0

Change your date formatter from @"yyyyMMdd to @"dd/MM/yyyy"

To learn Date Format Patterns read this doc

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184