Getting wrong date while add event in iPhone Reminder app from my application

104 Views Asked by At

I am adding an event with NSDate value

“2017-04-25 15:00:00 +0000”

As per my timezone, it’s 2017-04-25 08:40 PM.

I am getting NSDate value from a function.

NSString *strDateTime = @"Tuesday, 25 Apr 2017 08:30 PM";
NSDateFormatter *formatterLocal = [[NSDateFormatter alloc] init];
[formatterLocal setTimeZone:[NSTimeZone systemTimeZone]];
[formatterLocal setDateFormat:@"EEEE, dd MMM yyyy hh:mm a"];
NSDate *dateAdd = [formatterLocal dateFromString:strDateTime];

While I check reminder app it’s showing event with date 25/04/17, 3:00 PM. While it should be 25/04/17, 8:00 PM.

Can anyone please help me out from this!

I have already checked Get wrong time when adding an event to default calendar in iPhone

1

There are 1 best solutions below

0
kb920 On BEST ANSWER

I just catch my mistake.

I have set the wrong timezone in "dueDateComponents".

Below is my code.

EKReminder *reminder = [EKReminder reminderWithEventStore:self.eventStore];
reminder.dueDateComponents = [self dateComponentsForDefaultDueDate];

- (NSDateComponents *)dateComponentsForDefaultDueDate {
    /*
     Changing date components 
     */
    NSCalendar *cal = [NSCalendar currentCalendar];
    //[cal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDateComponents *components = [cal components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitHour | NSCalendarUnitMinute fromDate:[NSDate dateWithTimeIntervalSince1970:[_dictData[@"start"] longLongValue]]];

    components.hour = [components hour];
    components.minute = [components minute];

    return components;
}

I just commented

//[cal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

Silly mistake :p