我有一个函数来计算一周的开始和结束时间,它可以按预期工作。我想实现另一个函数,它可以计算出一天的开始和结束。我有下面的代码,但是我得到以下错误:
表达式类型不明确,没有更多上下文。

public class Date {
    let dateFormatter = NSDateFormatter()
    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()

    func calcStartAndEndDateForWeek(durationOccurance: Double) {
        print("Calculating start and end for week")
        let componentsWeek = calendar.components([.YearForWeekOfYear, .WeekOfYear], fromDate: date)
        let startOfWeek = calendar.dateFromComponents(componentsWeek)!

        print("start of Week = \(dateFormatter.stringFromDate(startOfWeek))")

        let componentsWeekEnds = NSDateComponents()
        componentsWeekEnds.weekOfYear = 1
        let endOfWeek = calendar.dateByAddingComponents(componentsWeekEnds, toDate: startOfWeek, options: [])!

        print("End of the week = \(dateFormatter.stringFromDate(endOfWeek))")
    }


    func calcStartAndEndDateForDay(durationOccurance: Double) {
        print("Calculating start and end for day")
        let componentsWeek = calendar.components([.Minutes, .Seconds], fromDate: date)
        let startOfDay = calendar.dateFromComponents(componentsWeek)!
        print("start day = \(dateFormatter.stringFromDate(startOfDay))")
    }

    init(){
        dateFormatter.dateFormat = "dd-MM-yyyy"
        }
   }

最佳答案

我们可以使用NSCalendar上的方法创建一个更通用的函数:

func rangeOfPeriod(period: NSCalendarUnit, date: NSDate) -> (NSDate, NSDate) {
    let calendar = NSCalendar.currentCalendar()
    var startDate: NSDate? = nil

    // let's ask calendar for the start of the period
    calendar.rangeOfUnit(period, startDate: &startDate, interval: nil, forDate: date)

    // end of this period is the start of the next period
    let endDate = calendar.dateByAddingUnit(period, value: 1, toDate: startDate!, options: [])

    // you can subtract 1 second if you want to make "Feb 1 00:00:00" into "Jan 31 23:59:59"
    // let endDate2 = calendar.dateByAddingUnit(.Second, value: -1, toDate: endDate!, options: [])

    return (startDate!, endDate!)
}

称为
 print("\(rangeOfPeriod(.WeekOfYear, date: NSDate()))")
 print("\(rangeOfPeriod(.Day, date: NSDate()))")

将其放入代码中:
public class Date {
    let dateFormatter = NSDateFormatter()
    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()

    func rangeOfPeriod(period: NSCalendarUnit) -> (NSDate, NSDate) {
        var startDate: NSDate? = nil

        calendar.rangeOfUnit(period, startDate: &startDate, interval: nil, forDate: date)

        let endDate = calendar.dateByAddingUnit(period, value: 1, toDate: startDate!, options: [])

        return (startDate!, endDate!)
    }

    func calcStartAndEndDateForWeek() {
        let (startOfWeek, endOfWeek) = rangeOfPeriod(.WeekOfYear)

        print("Start of week = \(dateFormatter.stringFromDate(startOfWeek))")
        print("End of the week = \(dateFormatter.stringFromDate(endOfWeek))")
    }


    func calcStartAndEndDateForDay() {
        let (startOfDay, endOfDay) = rangeOfPeriod(.Day)

        print("Start of day = \(dateFormatter.stringFromDate(startOfDay))")
        print("End of the day = \(dateFormatter.stringFromDate(endOfDay))")
    }

    init() {
        dateFormatter.dateFormat = "dd-MM-yyyy"
    }
}

let myDate = Date()
myDate.calcStartAndEndDateForWeek()
myDate.calcStartAndEndDateForDay()

10-06 14:50