Ankit Srivastava
Ankit Srivastava

Reputation: 12405

NSDate calculation missing 1 hour when date set manually from settings

I have a strange query...

If the date/time is set automatically in my iPhone than the time is correctly displayed as seen below... enter image description here

And if the date/time is set manually with a different time zone than I am actually in (I am in India and set it to London. Works fine if set to India),it shows wrong results only for two dates calculated by using the method [self.surveyModel.creationDate dateByAddingTimeInterval:60*60*24*30] and similarly for 20 days .. see image below..

enter image description here

I am not setting the default time zone or local time zone for the application, and I am suing the following method to show the result...

- (NSString *)stringWithFormat:(NSString *)format {
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [outputFormatter setDateFormat:format];
    NSString *timestamp_str = [outputFormatter stringFromDate:self];
    [outputFormatter release];
    return timestamp_str;
}

what am I doing wrong here...?

Upvotes: 1

Views: 283

Answers (1)

Steven
Steven

Reputation: 964

NOTE: This answer is in reply to the Comment on the Question

It seem the error is caused by regions with Daylight Savings, in this case London, England.

When the date/time is calculated automatically, the iOS DateTime library takes the DayLight Saving into account.

When dealing with the DateTime manually, which you are doing through this code [self.surveyModel.creationDate dateByAddingTimeInterval:60*60*24*30]

You are setting the DateTime without listing the DayLight option enabled. So when you take the NSTimeZone look for the DayLight savings options.

Upvotes: 2

Related Questions