Iphone NSDate和NSDate格式化程序
2012-10-31T21:38:00+05:30 我已经为iPhone实现了一个日历应用程序,其中我有一个字符串格式的日期(例如“2012-10-31T21:38:00+05:30”) 我想将其转换为NSDate 但我已经尝试了很多,但都没有成功 所以我想把这个日期格式转换成“MM DD YYYY” 有人能帮我使用这个功能吗Iphone NSDate和NSDate格式化程序,iphone,objective-c,Iphone,Objective C,2012-10-31T21:38:00+05:30 我已经为iPhone实现了一个日历应用程序,其中我有一个字符串格式的日期(例如“2012-10-31T21:38:00+05:30”) 我想将其转换为NSDate 但我已经尝试了很多,但都没有成功 所以我想把这个日期格式转换成“MM DD YYYY” 有人能帮我使用这个功能吗 -(NSDate*)mfDateFromDotNetJSONString:(NSString *)string { static NSRegularExpress
-(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;
}
希望它能帮助你 您需要NSDate对象还是只需要最后的“MM DD YYYY”字符串?