假设我有一个程序可以提醒用户他们的约会,从当前日期到约会日期,我想确定某个特定日期是约会的一周还是约会的一个月。
var startDate = startDate
let calendar = Calendar.current
let fmt = DateFormatter()
fmt.dateFormat = "yyyy-MM-dd"
while startDate <= endDate {
var newDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
if newDate is a month to endDate {
//schedule reminder
}
if newDate is a week to endDate{
//schedule reminder
}
我如何检查当前日期是否是约会的一周/一个月?
您不需要使用任何日期比较,您只需使用Calendar.date(byAdding:value:to:)
正确的组件即可生成通知日期。要将日期设置为1周/月之前endDate
,请传递-1
值。
let oneWeekBeforeAppointment = Calendar.current.date(byAdding: .weekOfYear, value: -1, to: endDate)!
let oneMonthBeforeAppointment = Calendar.current.date(byAdding: .month, value: -1, to: endDate)!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句