Skip to content

Instantly share code, notes, and snippets.

@shabbirh
Forked from MChorfa/A very short test
Last active August 29, 2015 14:10
Show Gist options
  • Save shabbirh/c71b6b01dc0b91b8f547 to your computer and use it in GitHub Desktop.
Save shabbirh/c71b6b01dc0b91b8f547 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment