我有一个应用程序,我想在周六或周日显示一些数据。 实际上我有一个分段控件,如果我按我的一个选项(这将是周末),我想检查它是星期六还是星期日,但只是第一个周末。
这就是我在分段控件中为获取当前日期所做的第一个选项
dateevent 是一个变量,我用来检查是否是 currentDate currentDate 声明为当前日期
if dateevent.earlierDate(self.currentDate).isEqualToDate(self.currentDate){
if NSCalendar.currentCalendar().isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day){
//Do something
}
}
最佳答案
首先找到要添加到 NSDateComponents
weekday
属性的天数,然后您可以使用 dateByAddingComponents(_:toDate:options:)
.
let today = NSDate()
let calendar = NSCalendar.currentCalendar()
let todayWeekday = calendar.component(.Weekday, fromDate: today)
let addWeekdays = 7 - todayWeekday // 7: Saturday number
var components = NSDateComponents()
components.weekday = addWeekdays
let nextSaturday = calendar.dateByAddingComponents(components, toDate: today, options: .MatchFirst)
关于ios - 我怎样才能找到下周末的 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38631176/