【问题标题】:Does dateComponents use 24 hour time? (scheduling local notifications)dateComponents 是否使用 24 小时制? (安排本地通知)
【发布时间】:2020-03-09 10:02:23
【问题描述】:

这段代码会创建一个通知触发器,它会在每天上午 10 点重复,对吧?

var dateComponents = DateComponents()
dateComponents.hour = 10
dateComponents.minute = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

将第 2 行更改为 dateComponents.hour = 22 设置为晚上 10 点吗?

【问题讨论】:

  • 是的。你还期望发生什么?顺便说一句,无需将分钟分量设置为零
  • 你尝试的时候发生了什么?

标签: swift nsdatecomponents hour


【解决方案1】:

是的。 DateComponents 默认使用 24 小时格式。

如果你想了解更多,我建议this 很棒的文章。

【讨论】:

  • 请注意,小时组件不限于 24 小时。你可以通过任何数量的小时。尝试DateComponents(calendar: .current, year: 2019, day: 1, hour: 40).date! 它将导致"Jan 2, 2019 at 4:00 PM"
  • @LeoDabus 这就是我问这个问题的原因。我知道您可以在未来 40 小时或 256 小时内设置一次通知,但对于在确切时间(即每天上午 10 点或每天晚上 10 点)设置重复通知,我不确定。我更习惯使用 timeIntervalSinceNow,所以通常我使用一个整数来表示添加到当前时间的时间,而不是将通知设置为精确的小时。
猜你喜欢
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多