当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - JSON date with offset to NSDate

问题描述:

I am developing an Cocoa application on the Mac and I am consuming a WCF service which is returning me a JSON date which comes from a SQL Server DATETIMEOFFSET field. The JSON object returned from the service including the date fields is outlined below:

{

"ObjectId": 1,

"Name": "My Object Name",

"Description": "My Object Description",

"CreateDate": {

"DateTime": "/Date(1306481778297)/",

"OffsetMinutes": 0

},

"LastModifiedDate": {

"DateTime": "/Date(1306936930427)/",

"OffsetMinutes": 0

}

}

My question is how do I convert the CreateDate and LastModifiedDate fields to NSDate objects?

I attempted to create a NSDateFormatter and abstract the value that way. However this works for simple string dates or the standard JSON date (/Date(1306936930427)/) but not the the date object containing the date and the offset.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]

NSString *creationDate = [myObject valueForKey:@"CreateDate"];

NSDate *date = [dateFormatter dateFromString:creationDate];

Any help on this one will be much appreciated.

网友答案:

First, extract the number from the string (e.g. 1306936930427); NSScanner might come in handy for this. Then divide it by 1000.0 and use NSDate's dateWithTimeIntervalSince1970: class method to get your NSDate.

分享给朋友:
您可能感兴趣的文章:
随机阅读: