-1

My server gives date like this

07/22/2013 06:13:04 PM UTC +05:30

I want to convert it to Device time zone.Like if my Device time zone is Local Time Zone (Asia/Kolkata (GMT+05:30) offset 19800) than what this date will look like

Searched on Google and try so much code but not have any luck. plz help me.

  • I guess this would help you. http://stackoverflow.com/questions/1268509/convert-utc-nsdate-to-local-timezone-objective-c – Piyush Dubey Jul 22 '13 at 13:08
  • Have a look at this also. http://stackoverflow.com/questions/1081647/how-to-convert-time-to-the-timezone-of-the-iphone-device?lq=1 – Piyush Dubey Jul 22 '13 at 13:15

1 Answers1

0

Your dateFormat does not match the format of your date string. Read about the Unicode Date Format Patterns. And after that try @"yyyy-MM-dd'T'HH:mm:ssZZZ"

But I think you try to do something wrong anyway. A NSDate is a point in time, it is the same for every place in the world. You don't need to calculate a new NSDate just because your source is in a different timezone.

Use the dateFormat from above and compare it directly to a different NSDate.

Probably something like this:

NSString *serverTime = @"2012-01-11T06:17:39+0000";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate* sourceDate = [dateFormatter dateFromString:serverTime];
if (!sourceDate) {
    NSLog(@"Wrong NSDateFormatter format!");
}
else {
    NSDate *date24HoursAgo = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];
    NSLog(@"%@", sourceDate);
    NSLog(@"%@", date24HoursAgo);
    if ([sourceDate compare:date24HoursAgo] == NSOrderedAscending) {
        NSLog(@"Timestamp from server is older than 24 hours.");
    }
}
Rajeev Barnwal
  • 1,349
  • 11
  • 14