0

i'm using [NSDate date] to get the current date but it is returning 5hrs 30 mts forward. it is returning different time before PM and after PM. My requirement is I want to delete the data in cache after 1 hr of duration.

At present I'm using following code to calculate the time difference. //storyDate is the one which is stored in cache...

NSTimeInterval timeGapInSeconds =fabs([[NSDate date] timeIntervalSinceDate:storyDate]); 
NSInteger daysGap= timeGapInSeconds/86400;
NSInteger hoursGap = timeGapInSeconds/3600;
NSInteger mtsGap = timeGapInSeconds/60;

NSLog(@"days gap %lu, hours gap is %lu",(long)daysGap,(long)hoursGap);
NSLog(@"time gap in seconds %f",timeGapInSeconds);
 NSLog(@"time gap in mins %f",timeGapInSeconds/60);
NSLog(@"current date:%@",[NSDate date]);
user2823044
  • 315
  • 2
  • 5
  • 14
  • 2
    That is due to your local time zone differing from GMT which is used when you NSLog a date – Volker Mar 25 '14 at 11:47
  • 4
    And don't use constants like 86400 for calendrical calculations (there are days with 23 or 25 hours - think of daylight saving time!). Use NSCalendar, NSDateComponents, ... – Martin R Mar 25 '14 at 11:50
  • 4
    That's because you're in India. – Hot Licks Mar 25 '14 at 11:51

1 Answers1

0

You can add this to your dateFormatter if you want to make it generalize :

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[NSLocale currentLocale]];

Or If you are making any application for particular country then use this :

NSLocale *usLoc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLoc];

Hope this would work.

Ahmed Z.
  • 2,329
  • 23
  • 52
Gaurav Govilkar
  • 154
  • 1
  • 1
  • 10