【问题标题】:NSDateFormatter does not format date and time correctlyNSDateFormatter 无法正确格式化日期和时间
【发布时间】:2015-04-13 08:23:52
【问题描述】:

在我的应用程序中,我为某些事件设置了本地通知。但是,NSDateFormatter 的日期和时间格式不正确。当我格式化日期时,它会提前 3 小时。同样,当我使用NSDate() 获取当前时间时,它会提前 3 小时作为 UTC。我的应用程序中的另一个事件也遇到了同样的问题,但我可以通过添加 dateFormatter.timeZone = NSTimeZone(name: "GMT") 来解决它。当前问题与以下代码有关:

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

在上面的代码中,我尝试将本地通知设置为“提醒”和“加入服务”。但是,我无法正确设置本地通知时间。

感谢您的回答

最好的问候

【问题讨论】:

  • 你必须展示你的其余代码。 SetUpLocalNotification()
  • 好的,我现在正在展示@LeonardoSavioDabus
  • 只需将您的 notifyAlarm.fireDate = notificationTime 移到 timeZone 行之后

标签: swift nsdateformatter uilocalnotification ios8.2 xcode6.3


【解决方案1】:

您只需在设置时区后设置您的 fireDate。

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

此外,您不应该将布尔变量与 true 进行比较。是多余的

if withCategory { 
    notifyAlarm.category  = "FIRST_CATEGORY"
}

您还需要从代码中删除 .dateByAddingTimeInterval(3*60*60)。

【讨论】:

  • 当我如上所述给出 timeZone 'dateFormatter.timeZone = NSTimeZone(name: "GMT")' 时,它会进一步设置本地通知。我应该使用哪种格式?
  • 始终使用本地时间。顺便说一句,这是默认设置。它总是只存储一个时间点 (GMT)。因此,如果您在某个偏移 3 小时的地方,它将存储相对 GMT 时间。
  • 好的,我会尝试'localTimeZone'并再次给您写信。谢谢
  • 问题可能是你使用 dateByAddingUnit 的方式
  • 尝试添加 dateFormatter2.timeZone = NSTimeZone.localTimeZone()
猜你喜欢
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 2011-12-11
  • 2017-04-10
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多