Skip to content

Instantly share code, notes, and snippets.

@exalted
Created December 3, 2010 12:42
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save exalted/726910 to your computer and use it in GitHub Desktop.
Save exalted/726910 to your computer and use it in GitHub Desktop.
This will convert DateTime (.NET) object serialized as JSON by WCF to a NSDate object
/*
* This will convert DateTime (.NET) object serialized as JSON by WCF to a NSDate object.
*/
// Input string is something like: "/Date(1292851800000+0100)/" where
// 1292851800000 is milliseconds since 1970 and +0100 is the timezone
NSString *inputString = [item objectForKey:@"DateTimeSession"];
// This will tell number of seconds to add according to your default timezone
// Note: if you don't care about timezone changes, just delete/comment it out
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
// 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
NSDate *date = [[NSDate dateWithTimeIntervalSince1970:
[[inputString substringWithRange:NSMakeRange(6, 10)] intValue]]
dateByAddingTimeInterval:offset];
// You can just stop here if all you care is a NSDate object from inputString,
// or see below on how to get a nice string representation from that date:
// static is nice if you will use same formatter again and again (for example in table cells)
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
// If you're okay with the default NSDateFormatterShortStyle then comment out two lines below
// or if you want four digit year, then this will do it:
NSString *fourDigitYearFormat = [[dateFormatter dateFormat]
stringByReplacingOccurrencesOfString:@"yy"
withString:@"yyyy"];
[dateFormatter setDateFormat:fourDigitYearFormat];
}
// There you have it:
NSString *outputString = [dateFormatter stringFromDate:date];
@tristandl
Copy link

doesn't this fail for dates earlier than Sun, 09 Sep 2001 01:46:40 GMT as the unix timestamp is only 9 characters (so you will read an extra 0 and have a massive error)?

@exalted
Copy link
Author

exalted commented Feb 20, 2011

@tristandl thanks a lot for you comment and excuse me for late reply. My code worked at the time for which I needed for, but I don't pretend it to be perfect. Are you just guessing or did you actually encountered this problem? If you think you can fix it any patches are very welcome. Regards.

@tristandl
Copy link

If the date is pre 2001 it will be less than 10 characters, and you will accidentally consume one of the trailing 0's. I appreciate you want to keep it lean and not parse the whole string to find the end of the text, see my fork for my modification where I look for the start of the number, and use doubleValue which reads the string up to the ), + or - character and divide by 1000 to get time in seconds.

In my project I removed the timezone stuff and format for locales in the UI, but that's up to you

@exalted
Copy link
Author

exalted commented Feb 22, 2011

@tristandl since I don't have time to try this I'll take your word about pre 2001 less than 10 chars issue and I'll agree with you. I knew a range of (6, 10) was just an ugly hack to do this, but I assumed JSON string was always "that much long"... Soonish I will create a repo for this and put some other little things of mine for Objective-C language, so I will get serious&deeper on this. Your fix, one way or another, will definitely be there. Thank a lot!

@exalted
Copy link
Author

exalted commented Feb 8, 2012

Check out also RestKit/RestKit#264

@Ded77
Copy link

Ded77 commented Jul 18, 2012

@exalted Thank you very much, it really helped me !

@exalted
Copy link
Author

exalted commented Jul 19, 2012

@Ded77 you're welcome... ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment