swiftnsdateformatteruilocalnotificationios8.2xcode6.3

NSDateFormatter does not format date and time correctly


In my app, I setup local notifications for some events. However, NSDateFormatter does not format date and time correctly. When I format a date, It gives 3 hours earlier. Similarly, when I get the current time using NSDate(), it gives 3 hours earlier as UTC. I faced the same problem for another event in my app, but I could solve it by adding dateFormatter.timeZone = NSTimeZone(name: "GMT"). The current issue is related to the code below:

var dateFormatter1 = NSDateFormatter()
var dateFormatter2 = NSDateFormatter()
dateFormatter1.dateFormat = "yyyy-MM-dd"
dateFormatter2.dateFormat = "yyyy-MM-dd HH:mm"

let mainItemList: Array<BagItem> = self.GetMainItemsInfoInBag()
for(item) in mainItemList
{
    let dateString = dateFormatter1.stringFromDate(item.date) + " " + item.time
    // Send servey notification 
    let serveyNotificationTime = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitHour, value: Int((120 + item.itemService.duration) / 60), toDate: dateFormatter2.dateFromString(dateString)!, options: NSCalendarOptions.WrapComponents)

    self.SetUpLocalNotification(serveyNotificationTime, message: "Do you want to join to the servey?": true)

    // if time for now is earlier than reservation time - 1 based on hour, set reminder notification.
    if(NSDate().dateByAddingTimeInterval(3*60*60).compare(NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitHour, value: -1, toDate: dateFormatter2.dateFromString(dateString)!, options: NSCalendarOptions.WrapComponents)!) == NSComparisonResult.OrderedAscending)
    {
         let reminderNotificationTime = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitHour, value: -1, toDate: dateFormatter2.dateFromString(dateString)!, options: NSCalendarOptions.WrapComponents)
         self.SetUpLocalNotification(reminderNotificationTime, message: message, withCategory: false)
    }
}

// Set Up Local Notifications
private func SetUpLocalNotification(notificationTime: NSDate!, message: String!, withCategory: Bool!)
{
    var app: UIApplication = UIApplication.sharedApplication()
    var notifyAlarm: UILocalNotification! = UILocalNotification()
    if(notifyAlarm != nil)
    {
        notifyAlarm.fireDate  = notificationTime
        notifyAlarm.timeZone  = NSTimeZone.defaultTimeZone()
        if(withCategory == true)
        {
            notifyAlarm.category  = "FIRST_CATEGORY"
        }
        notifyAlarm.soundName = UILocalNotificationDefaultSoundName
        notifyAlarm.alertBody = message
        app.scheduleLocalNotification(notifyAlarm)
    }
}

In the code above, I am trying to setup local notifications as "reminder" and "join servey". However, I can not set up local notification times correctly.

Thank you for your answers

Best regards


Solution

  • You just have to setup your fireDate after setting up your timeZone.

    notifyAlarm.timeZone = NSTimeZone.localTimeZone()
    notifyAlarm.fireDate = notificationTime
    

    Also you shouldn't compare a boolean var to true. It is redundant

    if withCategory { 
        notifyAlarm.category  = "FIRST_CATEGORY"
    }
    

    You will need to remove also the .dateByAddingTimeInterval(3*60*60) from your code.