gpt4 book ai didi

ios - 如何在特定时间和日期添加日历事件?

转载 作者:搜寻专家 更新时间:2023-11-01 06:33:49 26 4
gpt4 key购买 nike

  • 我从日期选择器中获取日期和时间,并将其设置为字符串格式。
  • 我想在给定的日期和时间添加事件。
  • 我想在给定的特定时间(例如“5 分钟、15 分钟、30 分钟”)之前设置两个警报,1 警报。 2 事件时间提醒。
  • 在下面的代码中,我需要做什么才能完成我的要求。

提出解决问题的建议或想法谢谢

从日期选择器获取日期和时间

let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "YYYY-MM-dd"
routineDate = dateFormatter.string(from: self.datetimepicker.date)
print(routineDate)

let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "HH:mm a"
routineTime = dateFormatter.string(from: self.datetimepicker.date)
print(routineTime)

在此处创建并保存 EKEvent:

 let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.dateFormat = "YYYY-MM-dd"
let startDATE = dateFormatter.date(from: routineDate)
let endDATE = dateFormatter.date(from: routineDate)

print(routineDate)
if temptypeID == "1" {
let event:EKEvent = EKEvent(eventStore: appleEventStore)
event.title = tempDescription

// what should i need to change here for specific date and time?

event.startDate = (startDATE?.addingTimeInterval(60*60))!
event.endDate = (endDATE?.addingTimeInterval(60*60))!
event.isAllDay = false
event.notes = "This is a note"
event.calendar = cal

do {
try appleEventStore.save(event, span: .thisEvent)
print("events added with dates:")

// what should changes i need to do here for alert ?

let alarm = EKAlarm(absoluteDate: Date(timeInterval: -3600, since: event.startDate))

event.addAlarm(alarm)

} catch let e as NSError {
print(e.description)
return
}
print("Saved Event")
}

最佳答案

尝试将您的日期保存为日期:

let startDate = self.datetimepicker.date
let endDate = startDate.addingTimeInterval(3600 as TimeInterval)

然后使用这些来创建您的事件:

let event:EKEvent = EKEvent(eventStore: appleEventStore)
event.title = tempDescription

// what should i need to change here for specific date and time?

event.startDate = startDate
event.endDate = endDate
event.isAllDay = false
event.notes = "This is a note"
event.calendar = cal

do {
try appleEventStore.save(event, span: .thisEvent)
print("events added with dates:")

// Adding an Alert 1 hour before the startDate
let reminder = EKAlarm(relativeOffset: -3600)
event.addAlarm(reminder)
print("Saved Event")
} catch let e as NSError {
print(e.description)
return
}

此外,事件的创建不应该在 DateFormatter 声明中。

关于ios - 如何在特定时间和日期添加日历事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44064560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com