2012-11-22 63 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; 
} 

希望它可以幫助你!

相關問題