Skip to content

Instantly share code, notes, and snippets.

@MChorfa
Forked from weejayuk/A very short test
Created August 9, 2011 22:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MChorfa/1135412 to your computer and use it in GitHub Desktop.
Save MChorfa/1135412 to your computer and use it in GitHub Desktop.
NSDate Extension to create .net JSON date strings and convert back. Testing for this was very limited, so use with caution. Some of the code was taken from here. http://www.pittle.org/weblog/how-to-convert-datetime-net-object-serialized-as-json-by-wcf-to-
NSDate *timenow=[NSDate date];
NSLog(@"Date before date2dot net=%@",[timenow description]);
NSString *date2DotNet=[timenow dateToDotNet];
NSLog(@"Dot net version of now = %@",date2DotNet);
timenow=[NSDate dateFromDotNet:date2DotNet];
NSLog(@"Date back from date2dot net=%@",[timenow description]);
#import <Foundation/Foundation.h>
@interface NSDate (DotNetDates)
+(NSDate*) dateFromDotNet:(NSString*)stringDate;
-(NSString*) dateToDotNet;
@end
#import "NSDate+DotNetDates.h"
@implementation NSDate (DotNetDates)
+(NSDate*) dateFromDotNet:(NSString*)stringDate{
NSDate *returnValue;
if ([stringDate isMemberOfClass:[NSNull class]]) {
returnValue=nil;
}
else {
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
returnValue= [[NSDate dateWithTimeIntervalSince1970:
[[stringDate substringWithRange:NSMakeRange(6, 10)] intValue]]
dateByAddingTimeInterval:offset];
}
return returnValue;
}
-(NSString*) dateToDotNet{
double timeSince1970=[self timeIntervalSince1970];
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
offset=offset/3600;
double nowMillis = 1000.0 * (timeSince1970);
NSString *dotNetDate=[NSString stringWithFormat:@"/Date(%.0f%+03d00)/",nowMillis,offset] ;
return dotNetDate;
}
@end
@brabbeldas
Copy link

I belief there is a mistake in the dateFromDotNet: method.
In this method the offset is added to the newly created NSDate object. I think this is a mistake.

Like in the dateToDotNet method, offset is just there for additional information. To let the other side know from which timeZone de date came.
The following should be sufficient in the dateFromDotNet: method (i split the code up a little for readability):

// The JSON string is the time-interval since 1970GTM //
NSString * subStr = [stringDate substringWithRange:NSMakeRange(6, 10)];
int timeInSec = [subStr intValue];
returnValue= [NSDate dateWithTimeIntervalSince1970:timeInSec ];

You should then use the offset to determine the timeZone. When determined you can apply this to the NSDate (in below example i assume the timeZone is Europe/Amsterdam):

// Specificly set the TimeZone to Europe/Amsterdam: //
// - Get DateComponents //
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSTimeZoneCalendarUnit;
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [cal components:unitFlags fromDate:returnValue];
// - Adjust DateComponent.TimeZone //
[comps setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Amsterdam"]]; //
// - Apply to NSDate //
returnValue = [cal dateFromComponents:comps];

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