在我的申请中,我可以为EKEvent
选择日期和时间。除了时间,我几乎达到了我想要的。我该如何设定时间。下面是我的代码,我试过了。
func setReminder(){
let eventStore = EKEventStore()
eventStore.requestAccess(
to: EKEntityType.event, completion: {(granted, error) in
if !granted {
print("Access to store not granted")
print(error!.localizedDescription)
} else {
print("Access granted")
let event = EKEvent(eventStore: eventStore)
event.title = self.getCategoryText + " Reminder"
event.calendar =
eventStore.defaultCalendarForNewEvents
event.timeZone = TimeZone.current
let alarm = EKAlarm(absoluteDate: self.selectedDate!)
event.addAlarm(alarm)
if self.daysBeforeDate != nil{
let earlierDate = self.daysBeforeDate
let earlierAlarm = EKAlarm(absoluteDate: earlierDate!)
event.addAlarm(earlierAlarm)
}
print("selected time", self.selectedTime)
var number1: Double = (self.selectedTime! as NSString).doubleValue
event.startDate = self.selectedDate!.addingTimeInterval(number1)
event.endDate = self.selectedDate!
do {
try eventStore.save(event, span: .thisEvent, commit: true)
} catch let error {
print("Reminder failed with error \(error.localizedDescription)")
return
}
}
})
}
更新1
let calendar = Calendar.current
var dateComponents = DateComponents()
dateComponents.year = calendar.component(.year, from: self.selectedDate!)
dateComponents.month = calendar.component(.month, from: self.selectedDate!)
dateComponents.day = calendar.component(.day, from: self.selectedDate!)
dateComponents.timeZone = TimeZone.current
dateComponents.hour = self.selectedHour
dateComponents.minute = self.selectedMinute
let userCalendar = Calendar.current // user calendar
let someDateTime = userCalendar.date(from: dateComponents)
答案 0 :(得分:0)
这是我用于创建一个时间间隔的事件的代码,它对我来说很好用:
@{
var isClient = true;
}
<script type="text/javascript">
if (@isClient)
{
function test() {
var arr = [1,2,3,4];
for (var i = 0; i < arr.length; i++) {
try {
var rep = "\";
console.log(arr[i].replace(new RegExp("'", "g"), rep));
}
catch (ex) {
throw "Exception occured during iteration"
}
}
}
}
else {
alert("else part")
}
</script>