我怎样才能找到下周末的斯威夫特

时间:2016-07-28 08:39:05

标签: ios swift date parsing

我有一个应用程序,我希望在周六或周日显示一些数据。 实际上我有一个分段控制,如果我按下我的一个选项(这将是周末)我想检查它是星期六或星期日,但只是第一个周末。

这就是我在分段控制中第一个选择当前日期的选项

dateevent是我用来检查是否是currentDate的变量 currentDate被声明为currentDate

  if  dateevent.earlierDate(self.currentDate).isEqualToDate(self.currentDate){

  if NSCalendar.currentCalendar().isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day){

                                //Do something
                            }

                        }

5 个答案:

答案 0 :(得分:4)

首先查找要添加到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)

答案 1 :(得分:3)

来自Apple docs

  

如果日期确实属于周末,您可以使用   rangeOfWeekendStartDate:interval:containingDate:方法确定   那个周末期间的开始日期。否则,你可以使用   nextWeekendStartDate:interval:options:afterDate:方法确定   下周末或上周末的开始日期。

答案 2 :(得分:0)

获取星期几的延期

func dayOfTheWeek() -> String? {
    let dateFormatter = NSDateFormatter()
    dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
    dateFormatter.dateFormat = "EEEE"
    return dateFormatter.stringFromDate(self)
}

然后您可以计算周末需要多少天并将其添加到NSDate

NSDate().dateByAddingTimeInterval(60 * 60 * 24 * daysFromTodayToWeekend)

答案 3 :(得分:0)

Swift 4解决方案

我已经根据自己的要求弄清楚了,在那里我找到了后续的日期。

1. Today

2. Tomorrow 

3. This Week 

4. This Weekend 

5. Next Week 

6. Next Weekend

因此,我创建了Date Extension来获取当前周下周的日期。

代码

extension Date {

    func getWeekDates() -> (thisWeek:[Date],nextWeek:[Date]) {
        var tuple: (thisWeek:[Date],nextWeek:[Date])
        var arrThisWeek: [Date] = []
        for i in 0..<7 {
            arrThisWeek.append(Calendar.current.date(byAdding: .day, value: i, to: startOfWeek)!)
        }
        var arrNextWeek: [Date] = []
        for i in 1...7 {
            arrNextWeek.append(Calendar.current.date(byAdding: .day, value: i, to: arrThisWeek.last!)!)
        }
        tuple = (thisWeek: arrThisWeek,nextWeek: arrNextWeek)
        return tuple
    }

    var tomorrow: Date {
        return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
    }
    var noon: Date {
        return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
    }

    var startOfWeek: Date {
        let gregorian = Calendar(identifier: .gregorian)
        let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
        return gregorian.date(byAdding: .day, value: 1, to: sunday!)!
    }

    func toDate(format: String) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
}

用法:

let arrWeekDates = Date().getWeekDates() // Get dates of Current and Next week.
let dateFormat = "MMM dd" // Date format
let thisMon = arrWeekDates.thisWeek.first!.toDate(format: dateFormat)
let thisSat = arrWeekDates.thisWeek[arrWeekDates.thisWeek.count - 2].toDate(format: dateFormat)
let thisSun = arrWeekDates.thisWeek[arrWeekDates.thisWeek.count - 1].toDate(format: dateFormat)

let nextMon = arrWeekDates.nextWeek.first!.toDate(format: dateFormat)
let nextSat = arrWeekDates.nextWeek[arrWeekDates.nextWeek.count - 2].toDate(format: dateFormat)
let nextSun = arrWeekDates.nextWeek[arrWeekDates.nextWeek.count - 1].toDate(format: dateFormat)

print("Today: \(Date().toDate(format: dateFormat))") // Sep 26
print("Tomorrow: \(Date().tomorrow.toDate(format: dateFormat))") // Sep 27
print("This Week: \(thisMon) - \(thisSun)") // Sep 24 - Sep 30
print("This Weekend: \(thisSat) - \(thisSun)") // Sep 29 - Sep 30
print("Next Week: \(nextMon) - \(nextSun)") // Oct 01 - Oct 07
print("Next Weekend: \(nextSat) - \(nextSun)") // Oct 06 - Oct 07

您可以根据需要修改Extension

谢谢!

答案 4 :(得分:0)

我使用这个实现来获取下一个特定时间的星期六:

代码 (Swift 5):

func nextSaturday(atHour hour: Int, min: Int) -> Date {
    let today = Date()
    let daysToAdd = 7 - (Calendar.current.dateComponents([.weekday], from: today).weekday ?? 0 )
    let nextSaturday = Calendar.current.date(byAdding: .day, value: daysToAdd, to: today)!
    return Calendar.current.date(bySettingHour: hour, minute: min, second: 0, of: nextSaturday)!
}

使用方法:

nextSaturday(atHour: 10, min: 0)