0

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.

Michał Zygar
  • 4,052
  • 1
  • 23
  • 36
  • That looks like a timestamp in milliseconds (1326063600000 -> Jan 8/12 5:00pm), with a 0000 (aka GMT/UTC) timezone offset. – Marc B Jan 09 '12 at 15:39
  • yes it is. I'm looking for elegant way to parse it. So far I figured a way of using a substring of this value as a timestamp(timeInterval). It's just a bit harsh – Michał Zygar Jan 09 '12 at 15:54

2 Answers2

2

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];
}
user511037
  • 147
  • 2
  • 3
1

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.

amattn
  • 10,045
  • 1
  • 36
  • 33
  • I have been looking for the better solution, but I guess I'll stick with that. Also +1 for seconds/miliseconds reminder – Michał Zygar Jan 10 '12 at 08:02