【问题标题】:Getting wrong date while add event in iPhone Reminder app from my application从我的应用程序在 iPhone 提醒应用程序中添加事件时获取错误日期
【发布时间】:2017-04-10 11:39:12
【问题描述】:

我正在添加一个带有 NSDate 值的事件

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

按照我的时区,现在是 2017-04-25 08:40 PM。

我正在从一个函数中获取 NSDate 值。

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];

当我检查提醒应用时,它显示日期为 25/04/17 下午 3:00 的事件。虽然应该是 25/04/17,晚上 8:00。

谁能帮我解决这个问题!

我已经查过了Get wrong time when adding an event to default calendar in iPhone

【问题讨论】:

    标签: objective-c nsdate ekeventstore ekeventkit ekreminder


    【解决方案1】:

    我刚刚发现我的错误。

    我在“dueDateComponents”中设置了错误的时区。

    下面是我的代码。

    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;
    }
    

    我刚刚评论了

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

    愚蠢的错误:p

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-02
      • 2011-12-20
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      相关资源
      最近更新 更多