How to parse /Date(xxx+0000)/ in SBJSON

1k views Asked by At

I am trying to parse JSON with date produced by the .NET service to NSDate object

{
 "Dashboard": {
    "Date": "/Date(1326063600000+0000)/"
    }
}

What is the approach of converting such formatted date using SBJSON? I extracted the value to NSString using

//dict is NSDictionary with Dashboard
NSString* dateString=[dict objectForKey:@"Date"];

and then stopped. Any suggestions wil be appreciated.

2

There are 2 answers

1
amattn On BEST ANSWER

From the comments, it already sounds like you've extracted the timestamp.

You probably want to use NSDate epoch constructor: dateWithTimeIntervalSince1970:

NSDate *date = [NSDate dateWithTimeIntervalSince1970:SOME_TIME_INTERVAL];

In this case, SOME_TIME_INTERVAL would be the NSTimeInterval (just an alias for double) value: 1326063600. Don't forget to divide the value you get by 1000. NSTimeIntervals are in seconds, not milliseconds.

0
user511037 On

This is the category I made for parsing from .NET

//parses a date from format "/Date(0000000000000+0000)/"
+(NSDate*)dateFromEpocString:(NSString*)epocString {

    if ([epocString isEqual:[NSNull null]])
        return nil;

    NSString* miliMinutes = [epocString substringWithRange:NSMakeRange(6,13)];
    long long minutesSince1970 = [miliMinutes longLongValue] / 1000;
    int timeOffset = [[epocString substringWithRange:NSMakeRange(19,3)] intValue];
    long long minutesOffset = timeOffset * 60 * 60;
    return [NSDate dateWithTimeIntervalSince1970:minutesSince1970 + minutesOffset];
}