Created
December 3, 2010 12:42
-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 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!
Check out also RestKit/RestKit#264
@exalted Thank you very much, it really helped me !
@Ded77 you're welcome... ;-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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