Search code examples
objective-cjsonnsdatensdateformatterutc

convert json utc "/Date(1420095600000-0700)/" date to nsdate


I get date like "/Data(xxx....xxx-xxxx)" from api. How can i get nsdate form this format. Temporarily I solved this problem to truncate the string and get seconds. but I need proper solution for this problem. kindly help me


Solution

  • Use NSDateFormater to transform this String (the parts within the brackets) into an NSDate and vice versa. See the docs here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/occ/instp/NSDateFormatter/dateFormat and here about the various formating possibilities: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/DataFormatting.html#//apple_ref/doc/uid/10000029i

    Well, you may have to get rid of the milliseconds.