【问题标题】:iPhone SDK - EventKit time zone issueiPhone SDK - EventKit 时区问题
【发布时间】:2012-05-08 15:38:17
【问题描述】:

在我的 iPhone 应用程序中将事件添加到日历时遇到了一些问题。

我正在使用代码:

EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title     = self.event.eventTitle;
event.startDate = self.event.startDate;
event.endDate   = self.event.endDate;
event.notes     = self.event.description;

[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;   
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];

在我的日历中添加一个event 就好了。问题是我的时区显然是关闭的。例如,self.event.startdate 在调试器中输出为012-05-11 20:45:00 +0000,这是正确的。 但是,当我查看我的日历时,该事件是在 4:45 PM(不是 20:458:45 PM应该是)。

当我使用 po 事件输出整个事件对象时,我得到:

EKEvent <0xf853320> {EKEvent <0xf853320> {title = Tuesday Night Lights!; location = (null); calendar = (null); alarms = (null); URL = (null); lastModified = (null); timeZone = EST (GMT-05:00) offset -18000}; location = (null); startDate = 2012-05-11 20:45:00 +0000; endDate = 2012-05-11 21:45:00 +0000; allDay = 0; floating = 0; recurrence = (null); attendees = (null)}

这里的startDate 也可以正确读取。我位于 EST 时区(我的手机也是如此),那么如何获取代码以在晚上 8:45 将活动添加到我的日历?

任何想法都会非常有帮助!

【问题讨论】:

    标签: iphone objective-c ios4


    【解决方案1】:

    事件是在正确的时间添加的。这只是用正确的时区校正显示时间的问题。我发现这解决了我的问题:

    不要这样写:

    NSLog(@"startDate: %@", [startDate description]);
    

    这样写:

    NSLog(@"startDate: %@", [startDate descriptionWithLocale:[NSLocale currentLocale]]);
    

    希望这可以帮助您解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2011-05-14
      • 2011-08-20
      相关资源
      最近更新 更多