2

I am getting some UTC date strings from WCF Rest service and here is the format:

/Date(1354851639500+0530)/

I used the following code to convert the date:

//jsonDateString = 1354851639500+0530

NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT]; //get number of seconds to add or subtract according to the client default time zone

NSTimeInterval unixTime = [[jsonDateString substringWithRange:NSMakeRange(0, 13)] doubleValue] / 1000; //WCF will send 13 digit-long value for the time interval since 1970 (millisecond precision) whereas iOS works with 10 digit-long values (second precision), hence the divide by 1000

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a ZZZZ"];
NSString *stringFromDAte = [dateFormatter stringFromDate:[[NSDate dateWithTimeIntervalSince1970:unixTime] dateByAddingTimeInterval:offset]];
NSLog(@"Server GMT: %@", stringFromDAte);


NSDate *currentDadte = [dateFormatter dateFromString:stringFromDAte];
NSTimeInterval interval = [currentDadte timeIntervalSinceDate:[NSDate date]];

return [self dailyLanguage:interval];

But when I convert the time is not correct. I need to get the UTC time of the receiving time. But I am getting the time value without the offset value.

For example: if josnDate = 1354851639500+0530, i am getting, 2012-12-07 03:40:39 AM GMT, but i should get 2012-12-07 09:10:39 (approx).

How can I do this? Please help.

Mithuzz
  • 1,091
  • 14
  • 43
  • Observation: If you're going to use dates from the past it's dangerous to use NSMakeRange(0,13). – Hot Licks Dec 07 '12 at 12:50
  • Observation: It's not clear whether your incoming time value is timezone-adjusted or not. If it's timezone-adjusted (not UTC) you need to subtract the incoming timezone offset. – Hot Licks Dec 07 '12 at 12:52
  • At the server, it is getting the current time and converting it to UTC time format. – Mithuzz Dec 07 '12 at 12:53
  • the `+0530` is the different between the two dates. it makes the sense. – holex Dec 07 '12 at 13:49
  • Convert the offset to NSDate without any adjustments and NSLog it, to see what it's converting to. – Hot Licks Dec 07 '12 at 16:26
  • Check this answer by jasongregori [parse JSON date](http://stackoverflow.com/questions/1757303/parsing-json-dates-on-iphone ) it worked like a charm for me. – Meera Apr 02 '13 at 10:45

2 Answers2

1

Try this Code

- (NSDate *)deserializeJsonDateString: (NSString *)jsonDateString
{
    NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT]; //get number of seconds to add or subtract according to the client default time zone 

    NSInteger startPosition = [jsonDateString rangeOfString:@"("].location + 1; //start of the date value

    NSTimeInterval unixTime = [[jsonDateString substringWithRange:NSMakeRange(startPosition, 13)] doubleValue] / 1000; //WCF will send 13 digit-long value for the time interval since 1970 (millisecond precision) whereas iOS works with 10 digit-long values (second precision), hence the divide by 1000

    [[NSDate dateWithTimeIntervalSince1970:unixTime] dateByAddingTimeInterval:offset];

    return date;
}

For more detail answer follow this link http://goo.gl/lE6ut

Bhavesh Bansal
  • 101
  • 1
  • 8
0

You can try this

NSString   *jsonDateString = [SingleResponseObject objectForKey:@"datetime"];;
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval unixTime = [[jsonDateString substringWithRange:NSMakeRange(0, 13)] doubleValue] / 1000;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a ZZZZ"];
NSString *stringFromDAte = [dateFormatter stringFromDate:[[NSDate dateWithTimeIntervalSince1970:unixTime] dateByAddingTimeInterval:offset]];
cell.date.text = stringFromDAte;
itsji10dra
  • 4,603
  • 3
  • 39
  • 59