如何将这种类型的日期格式转换为 yyyy-MM-dd HH:MM:ss 格式。
"DATE_OF_JOINING":"\/Date(1349634600000+0530)\/"
如何将这种类型的日期格式转换为 yyyy-MM-dd HH:MM:ss 格式。
"DATE_OF_JOINING":"\/Date(1349634600000+0530)\/"
使用 NSDateFormatter。这将让您以多种不同的方式将其格式化,并将其格式化为字符串。
这是苹果对 iOS 类的参考: https ://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
我更喜欢扫描仪而不是正则表达式来从字符串中提取时间间隔
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];