我在这个问题上寻求帮助,但我的答案还不够。我正在使用代码设置事件提醒。使用断点和踩踏。我看到活动时间是正确的。以下是我设置提醒的方式
EKEventStore *eventDB = [[[EKEventStore alloc] init]autorelease];
EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB];
NSString * eventTitle = [NSString stringWithFormat:@"%@ - %@",app.dealerBusinessName,serviceOrComments.text];
myEvent.title = eventTitle;
// "destinationDate" is the date I want to set the reminder for it is correct in debugger
//its format is 2011-06-03 15:45:58 +0000 which means (i would think) that the reminder
//should be set for 6/3/2011 3:45PM but its always 6 hours earlier (in this case at 9:45am
NSLog(@"value: %@",destinationDate);
myEvent.startDate = [[[NSDate alloc] initWithTimeInterval:0 sinceDate:destinationDate ]autorelease];
myEvent.endDate = [[[NSDate alloc] initWithTimeInterval:3600 sinceDate:myEvent.startDate]autorelease];
myEvent.allDay = NO;
myAlarmsArray = [[NSMutableArray alloc] init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-3600]; // 1 Hour
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-86400]; // 1 Day
[myAlarmsArray addObject:alarm1];
[myAlarmsArray addObject:alarm2];
myEvent.alarms = myAlarmsArray;
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
NSError *err;
[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];
if (err == noErr) {
//no error, but do not show alert because we do that below.
}
现在我非常确定我可以只增加21,600,但这是正确的方法吗?我不明白NSDate是如何运作的,它让我疯狂。有人可以向我解释一下这样做的正确方法是什么?谢谢!
答案 0 :(得分:1)
我最近遇到了类似的问题,这真让我疯了。您必须考虑您的时区(或模拟器/设备设置的时区)。 NSDate表示GMT时间,NSCalendar表示时区内的时间。因此,您所遇到的时差恰好是您的时区与格林尼治标准时间之间的时差(您的闹钟会在格林尼治标准时间15:45触发)。
由于我希望我的日期是绝对的,无论时区如何,我都将我的所有NSCalendars设置为GMT - 它适用于我的情况,当然是YMMV。