2012-11-22 54 views
0

2012-10-31T21:38:00 + 05:30的NSDate和NSDateFormatter

我已经实现为iPhone,其中我在字符串格式(例如,“2012-10-31T21日期的日历应用: 38:00 + 05:30" )。

我想将其转换为NSDate。

但我已经尝试了很多,没有成功。

所以我想将这个日期格式转换为“MM DD YYYY”。

任何人可以帮助我....

+0

你需要一个NSDate对象还是只需要最后的“MM DD YYYY”字符串? – rmaddy

回答

2

使用此功能

-(NSDate*)mfDateFromDotNetJSONString:(NSString *)string 
{ 
    static NSRegularExpression *dateRegEx = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     dateRegEx = [[NSRegularExpression alloc] initWithPattern:@"^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$" options:NSRegularExpressionCaseInsensitive error:nil]; 
    }); 
    NSTextCheckingResult *regexResult = [dateRegEx firstMatchInString:string options:0 range:NSMakeRange(0, [string length])]; 

    if (regexResult) 
    { 
     // milliseconds 
     NSTimeInterval seconds = [[string substringWithRange:[regexResult rangeAtIndex:1]] doubleValue]/1000.0; 
     // timezone offset 
     if ([regexResult rangeAtIndex:2].location != NSNotFound) { 
      NSString *sign = [string substringWithRange:[regexResult rangeAtIndex:2]]; 
      // hours 
      seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:3]]] doubleValue] * 60.0 * 60.0; 
      // minutes 
      seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:4]]] doubleValue] * 60.0; 
     } 

     return [NSDate dateWithTimeIntervalSince1970:seconds]; 
    } 
    return nil; 
} 

希望它可以帮助你!

相关问题