本周的天数列表(List of Days of this week)
我需要一个星期几的列表(周一从周一开始)列出时间卡列表。 我不知道如何实现这一目标。 我已经尝试了一些dateByAddingUnit并从那里减去但它没有产生我想要的结果。
知道如何获取日期列表吗? (即周一将于2016年3月28日星期二,2016年3月29日星期二等等)
I need a list of days of the week (with the week starting on Monday) for a timecard list. Im not sure how to achieve this. I have tried some dateByAddingUnit and subtracting from there but it hasn't produced the results I want.
Any idea how to get a list of the dates? (i.e. Monday would be 3/28/16, Tuesday 3/29/16 and so on)
最满意答案
编辑 :错过了你想要一个星期总是在星期一开始
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! let today = gregorian.startOfDayForDate(NSDate()) let startOfWeek: NSDate if gregorian.component(.Weekday, fromDate: today) == 2 { // Today is Monday startOfWeek = today } else { // Find the last Monday prior to today startOfWeek = gregorian.nextDateAfterDate(today, matchingUnit: .Weekday, value: 2, options: [.SearchBackwards, .MatchPreviousTimePreservingSmallerUnits])! }Edit: missed that you want a week to always start on a Monday
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! let today = gregorian.startOfDayForDate(NSDate()) let startOfWeek: NSDate if gregorian.component(.Weekday, fromDate: today) == 2 { // Today is Monday startOfWeek = today } else { // Find the last Monday prior to today startOfWeek = gregorian.nextDateAfterDate(today, matchingUnit: .Weekday, value: 2, options: [.SearchBackwards, .MatchPreviousTimePreservingSmallerUnits])! }更多推荐
发布评论