EKEventStore 通过了 NSDate 并减去了几个月

发布于 2024-12-17 10:21:01 字数 1100 浏览 2 评论 0原文

我正在努力向 iPhone 日历添加一个活动,一切都按计划进行,但是,我的开始日期减去了一个半月多一点。当日期实际上是 2012 年 2 月 14 日时...添加到日历中的事件日期是 2011 年 12 月 25 日。expiresOn.text

是一个类似于“Feb 14, 2012”的字符串。任何人都可以提供有关可能导致问题的原因的见解吗?

EKEventStore *eventStore = [[EKEventStore alloc] init];

EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title     = [NSString stringWithFormat: @"%@ Expiration", self.navigationItem.title];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 

NSString *dateString = [NSString stringWithFormat:@"0800 %@", expiresOn.text];

[dateFormat setDateFormat:@"hhmm MMM dd, YYYY"];

NSDate *date = [dateFormat dateFromString:dateString];

event.startDate = date;
event.endDate   = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];

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

EKEventViewController *viewController = [[EKEventViewController alloc] initWithNibName:nil bundle: nil];
viewController.event = event;
viewController.allowsEditing = YES;

I'm working to add an event to the iPhone calendar and everything goes as planned, however, a little over a month and a half is being subtracted from my startDate. When the date is actually Feb 14, 2012... the event date added to the calendar is Dec 25, 2011.

expiresOn.text is a string like "Feb 14, 2012". Can anyone offer insight as to what may be causing the problem?

EKEventStore *eventStore = [[EKEventStore alloc] init];

EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title     = [NSString stringWithFormat: @"%@ Expiration", self.navigationItem.title];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 

NSString *dateString = [NSString stringWithFormat:@"0800 %@", expiresOn.text];

[dateFormat setDateFormat:@"hhmm MMM dd, YYYY"];

NSDate *date = [dateFormat dateFromString:dateString];

event.startDate = date;
event.endDate   = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];

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

EKEventViewController *viewController = [[EKEventViewController alloc] initWithNibName:nil bundle: nil];
viewController.event = event;
viewController.allowsEditing = YES;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2024-12-24 10:21:01

您只是在格式化程序的设置中存在区分大小写的拼写错误。

使用:

[dateFormat setDateFormat:@"MMM dd, yyyy"];

我希望这会有所帮助!

You simply have a case sensitive typo in the set up for your formatter.

Use:

[dateFormat setDateFormat:@"MMM dd, yyyy"];

And I hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文