简体   繁体   English

我怎么知道特定日期是一个月还是一个星期到父母日期?

[英]How do i know if a specific date is a month, or a week to an parent date?

Lets say i have a program that reminds users of their appointments , from the current date until the date of the appointment, i want to find out if a particular date is a week to the appointment or a month to the appointment .假设我有一个程序可以提醒用户他们的约会,从当前日期到约会日期,我想知道特定日期是约会的一周还是约会的一个月。

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
    }

how can i check if the current date is a week/month to the appointment ?我如何检查当前日期是否是约会的一周/一个月?

You don't need to use any date comparison, you can simply generate the notification dates using Calendar.date(byAdding:value:to:) and just passing the correct components.您不需要使用任何日期比较,您可以简单地使用Calendar.date(byAdding:value:to:)生成通知日期并传递正确的组件。 To set the date 1 week/month before endDate , pass -1 to value.要在endDate之前 1 周/月设置日期,请将-1传递给值。

let oneWeekBeforeAppointment = Calendar.current.date(byAdding: .weekOfYear, value: -1, to: endDate)!
let oneMonthBeforeAppointment = Calendar.current.date(byAdding: .month, value: -1, to: endDate)!

Try this to calculate the duration in days试试这个来计算以天为单位的持续时间

func DateFormat() -> DateFormatter {
  let dateFormatter = DateFormatter()
  dateFormatter.dateFormat = "dd/MM/yyyy"
  dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
  return dateFormatter
}

var appointementDate: Date?
var today = Date()

appointementDate = DateFormat().date(from: "22/02/2020")
today = DateFormat().date(from: DateFormat().string(from: today))!

let timeInterval = Int(exactly: (today.timeIntervalSince(appointementDate!))) ?? 0
print("\(timeInterval/86400) days left")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道给定的日期是哪个月的哪个星期? - How to know the given date is which month's which week? 如何从给定日期获取月份的星期几 - How to get week of month from given date 如何使用任何月份和周数 swift 3 获取周开始日期和结束日期? - How to get week start date and end date by using any Month and week number swift 3? 我如何知道iPhone日期日历中的日期是否更改? - How do I know if day is changed in iPhone date calender? 是 swift 中另一个日期的同一周、月、年的日期 - Is a date in same week, month, year of another date in swift 如何从本周的工作日名称中获取特定日期 - How to get specific date from weekday name in this week 如何检测用户何时更改JTCalendar中的月份或星期? - How do I detect when a user changes the month or week in JTCalendar? 在datepicker中选择特定日期后,如何自动填充一周中的特定日期? - when particular date is selected in datepicker, how do i populate the particular day of week automatically? 如何使用sqlite获取一个月的数据? 如果没有特定日期的数据,那我希望在该位置上将零 - How to get data for a month using sqlite ? If there is no data for a specific date then i want zero on place of that iPhone OS:如何为特定日期创建NSDate? - iPhone OS: How do I create an NSDate for a specific date?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM