从NSIOS字符串获取日期
我的字符串包含::从NSIOS字符串获取日期,ios,objective-c,Ios,Objective C,我的字符串包含:: [{"temperature":"96.800003","date":"2015-11-26 18:44:42","random_id":"-1963708530","temp_type":"0","drugsymptom_value":""}] 要求: 如何仅从上述字符串中获取日期值?我将首先将其转换为JSON格式,然后您可以使用NSDateFormatter步骤1 将JSON字符串转换为简单字符串,如下所示 NSString *yourString = [{&quo
[{"temperature":"96.800003","date":"2015-11-26 18:44:42","random_id":"-1963708530","temp_type":"0","drugsymptom_value":""}]
要求:
如何仅从上述字符串中获取日期值?我将首先将其转换为JSON格式,然后您可以使用
NSDateFormatter
步骤1
将JSON字符串转换为简单字符串,如下所示
NSString *yourString = [{"temperature":"96.800003","date":"2015-11-26 18:44:42","random_id":"-1963708530","temp_type":"0","drugsymptom_value":""}];
NSData *jsonData = [yourString dataUsingEncoding:NSUTF8StringEncoding];
// your yourString is started with array not a Dictionary
NSArray *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:nil];
// now you have Date String
NSString *DateSTring = dict[0][@"date"];
步骤2
你的字符串是
2015-11-26 18:44:42
yyyy-MM-dd HH:mm:ss
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *finaldate=[dateFormatter dateFromString:DateSTring];
NSLog(@" finaldate %@",finaldate);
您的字符串是静态的还是动态的。。。