T M Nithi Anand
T M Nithi Anand

Reputation: 21

Gettings Date from the NSString ios

My string contains ::

 [{"temperature":"96.800003","date":"2015-11-26 18:44:42","random_id":"-1963708530","temp_type":"0","drugsymptom_value":""}]

Requirement : How can i get only the date value from the above string

Upvotes: 1

Views: 60

Answers (2)

chiarotto.alessandro
chiarotto.alessandro

Reputation: 1589

I would trasform it in a JSON first, then you can use NSDateFormatter.

Upvotes: -1

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

Step-1

convert your JSON String to Simple string just like

 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"];

Step-2

your string as

 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);

Upvotes: 2

Related Questions