转换date格式
如何将这种types的date格式转换为yyyy-MM-dd HH:MM:ss格式。
"DATE_OF_JOINING":"\/Date(1349634600000+0530)\/"
我宁愿使用正则expression式的扫描器来从string中提取时间间隔
NSDictionary *dict = @{@"DATE_OF_JOINING" : @"Date(1357683534626+0530)"}; NSString *string = dict[@"DATE_OF_JOINING"]; NSString *intervalString; NSString *offsetString; NSScanner *scanner = [NSScanner scannerWithString:string]; [scanner setScanLocation:[string rangeOfString:@"("].location+1]; [scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"+-"] intoString:&intervalString]; [scanner scanUpToString:@")" intoString:&offsetString]; NSInteger hours = [[offsetString substringToIndex:3] integerValue]; NSInteger minutes = [[offsetString substringFromIndex:3] integerValue]; NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; offsetComponents.hour = hours; offsetComponents.minute = minutes; NSTimeInterval interval= [intervalString doubleValue]; NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval/1000]; date = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:date options:0]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *result = [formatter stringFromDate:date];
使用NSDateFormatter。 这可以让你以不同的方式格式化成string。
这里是苹果的iOS类的参考: https : //developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html