Nsdate Conversion返回Null

时间:2013-10-18 07:15:56

标签: iphone ios objective-c

我正在尝试将字符串转换为日期,但它返回null。

  NSString *offer_publishdate*=@"15/10/2013";
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyyMMdd"];
        NSDate *date = [dateFormat dateFromString:offer_publishdate];
          NSLog(@"date:%@",date);

3 个答案:

答案 0 :(得分:1)

请选择确切的日期格式

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

答案 1 :(得分:0)

试试这种方式......

 NSString *offer_publishdate*=@"15/10/2013";
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"dd/MM/yyyy"];
        NSDate *date = [dateFormat dateFromString:offer_publishdate];

答案 2 :(得分:0)

将日期格式化程序从@"yyyyMMdd更改为@"dd/MM/yyyy"

To learn Date Format Patterns read this doc