現在の日付から予定の日付まで、ユーザーに予定を思い出させるプログラムがあるとしましょう。特定の日付が予定の1週間か、1か月かを調べたいと思います。
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]
コメントを追加