获取一周的日期

时间:2018-09-28 01:58:03

标签: ios swift nsdate

仅当我按特定日期时,才需要获取当月一周中的所有日期。例如,如果我按9月28日,结果将是9月23日至28日,但是如果我按9月30日,结果将仅是9月30日,因为此后的日期是10月。

这是我获取一周日期的代码:

extension Date {
    var startOfWeek: Date? {
        let gregorian = Calendar(identifier: .gregorian)
        guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
        return gregorian.date(byAdding: .day, value: 0, to: sunday)
    }

    var endOfWeek: Date? {
        let gregorian = Calendar(identifier: .gregorian)
        guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
        return gregorian.date(byAdding: .day, value: 6, to: sunday)
    }
}

但是对于此代码,结果是9月30日至10月6日,这不是我想要的。请帮助

3 个答案:

答案 0 :(得分:0)

您可以获取该月的最后一天,然后endOfWeek将是您已拥有的到月末的最小值。

var endOfWeek: Date? {
    let gregorian = Calendar(identifier: .gregorian)
    guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
    let endOfWeek = gregorian.date(byAdding: .day, value: 6, to: sunday)
    let firstOfNextMonth = gregorian.date(byAdding: .month, value: 1, to: sunday)
    let lastOfThisMonth = gregorian.date(byAdding: .day: value: -1, to: firstOfNextMonth)

    return min(endOfWeek, lastOfThisMonth)
}

我不能保证该代码可以编译,但是它应该使您了解如何完成所需的工作。

答案 1 :(得分:0)

我想推荐SwiftMoment,因为它是Moment.js的Swift端口。

Moment.js是一个功能丰富的库,具有出色的文档和超有用的功能。

给出一个date,就可以得到start of week Dateend of week Date

然后从那里开始,您可以枚举它们之间的日期。

我目前正在我的Web应用程序项目中使用Moment.js,此功能用于获取指定日期的星期日期:

getWeekDates (dateReference, format) {
    const startOfWeek = moment(dateReference).startOf('isoWeek');
    const endOfWeek = moment(dateReference).endOf('isoWeek');

    var days = [];
    var day = startOfWeek;

    while (day <= endOfWeek) {
        if (format !== undefined) {
          days.push(day.format(format));
        } else {
          days.push(day);
        }
        day = day.clone().add(1, 'd');
    }

    return days;
  }

答案 2 :(得分:0)

您可以将包含日期的星期和月份的间隔相交。

这是一个快速测试:

let calendar = Calendar(identifier: .gregorian)
let date = DateComponents(calendar: calendar, year: 2018, month: 9, day: 30).date!

let weekComponents = calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: date)
let startOfWeek = calendar.date(from: weekComponents)!
let endOfWeek = calendar.date(byAdding: .weekOfYear, value: 1, to: startOfWeek)!
let weekInterval = DateInterval(start: startOfWeek, end: endOfWeek)

let monthComponents = calendar.dateComponents([.year, .month], from: date)
let startOfMonth = calendar.date(from: monthComponents)!
let endOfMonth = calendar.date(byAdding: .month, value: 1, to: startOfMonth)!
let monthInterval = DateInterval(start: startOfMonth, end: endOfMonth)

let targetRange = monthInterval.intersection(with: weekInterval)!
let firstDay = targetRange.start
let lastDay = calendar.date(byAdding: .day, value: -1, to: targetRange.end)

您将希望进行重构以避免强制展开所有日期。