Skip to content

Instantly share code, notes, and snippets.

@nestserau
Forked from exalted/gist:726910
Created January 18, 2011 14:07
Show Gist options
  • Save nestserau/784471 to your computer and use it in GitHub Desktop.
Save nestserau/784471 to your computer and use it in GitHub Desktop.
/**
* This will convert DateTime (.NET) object serialized as JSON by WCF to a NSDate object.
*/
+ (NSDate *)dateFromJSONTicks:(NSString *)inputString;
+ (NSDate *)dateFromJSONTicks:(NSString *)inputString
{
/* Input string is something like: "/Date(1292851800000+0100)/" where
1292851800000 is milliseconds since 1970 and +0100 is the timezone. */
/* A range of NSMakeRange(6, 10) will generate "1292851800" from "/Date(1292851800000+0100)/"
as in example above. We crop additional three zeros, because "dateWithTimeIntervalSince1970:"
wants seconds, not milliseconds; since 1 second is equal to 1000 milliseconds, this will work.
Note: if you don't care about timezone changes, just chop out "dateByAddingTimeInterval:offset" part. */
NSTimeInterval timeInterval = [[inputString substringWithRange:NSMakeRange(6, 10)] doubleValue];
NSDate *result = [NSDate dateWithTimeIntervalSince1970:timeInterval];
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment