2017-06-17 51 views

回答

3

A DateComponents有一個weekday屬性,表示星期幾。平日(在基金會的公曆中)週日編號爲1,週一爲2,...,週六爲7。

一個DateComponents也有weekdayOrdinal屬性,代表「the position of the weekday within the next larger calendar unit, such as the month. For example, 2 is the weekday ordinal unit for the second Friday of the month.

讓我們初始化DateComponents一些星期六六月2017年它通常是一個好主意,以指定中午的時間,如果你不關心時間,因爲midnight (the default time of day) can cause problems in some time zones on some days

var components = DateComponents(era: 1, year: 2017, month: 06, hour: 12, weekday: 7) 

然後讓我們製作一個日曆。

var calendar = Calendar.autoupdatingCurrent 

現在我們可以遍歷所有可能的週日序數。對於每一個,我們都會要求日曆生成一個日期。然後我們要求日曆將日期轉換回年,月,日組件。

在公曆中,有些月份有5個星期六,但大多數有4個。所以當我們要求第5個星期六時,我們可能會在下個月得到一個日期。當發生這種情況時,我們想壓制那個日期。

for i in 1 ... 5 { 
    components.weekdayOrdinal = i 
    let date = calendar.date(from: components)! 
    let ymd = calendar.dateComponents([.year, .month, .day], from: date) 
    guard ymd.month == components.month else { break } 
    print("\(ymd.year!)-\(ymd.month!)-\(ymd.day!)") 
} 

輸出:

2017-6-3 
2017-6-10 
2017-6-17 
2017-6-24 

Objective-C的版本:

NSDateComponents *components = [NSDateComponents new]; 
components.era = 1; 
components.year = 2017; 
components.month = 6; 
components.hour = 12; 
components.weekday = 7; 
NSCalendar *calendar = NSCalendar.autoupdatingCurrentCalendar; 

for (NSInteger i = 1; i <= 5; ++i) { 
    components.weekdayOrdinal = i; 
    NSDate *date = [calendar dateFromComponents:components]; 
    NSDateComponents *ymd = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; 
    if (ymd.month != components.month) { break; } 
    NSLog(@"%ld-%ld-%ld", (long)ymd.year, (long)ymd.month, (long)ymd.day); 
} 
0

這是使用calendar方法稱爲enumerateDates和使用Date擴展

//month in MM format, year in yyyy format and dayNumber as Int 1 for sunday, 7 for saturday 
    func datesWith(dayNumber:Int,month:String,year:String) -> [Date] 
    { 
     assert(dayNumber >= 1 && dayNumber <= 7, "Day number is wrong") 

     let dateFormatter = DateFormatter() 
     dateFormatter.dateFormat = "yyyy-MM-dd" 
     let date = dateFormatter.date(from: year + "-" + month + "-" + "01") 

     guard date != nil else { 
      return [] 
     } 
     var resultDates : [Date] = [] 

     //check if firstDay of month is desired weekday 
     if(Calendar.current.component(.weekday, from: date!) == dayNumber) 
     { 
      resultDates.append(date!) 
     } 

     Calendar.current.enumerateDates(startingAfter: date!, matching: DateComponents(weekday: dayNumber), matchingPolicy: Calendar.MatchingPolicy.nextTimePreservingSmallerComponents) { (currentDate, result, stop) in 
      if(currentDate! > date!.endOfMonth()) 
      { 
       stop = true 
       return 
      } 
      resultDates.append(currentDate!) 
     } 

     return resultDates 
    } 
你的問題的另一個解決方案

擴展

extension Date { 
    func startOfMonth() -> Date { 
     return Calendar.current.date(from: Calendar.current.dateComponents([.year, .month], from: Calendar.current.startOfDay(for: self)))! 
    } 

    func endOfMonth() -> Date { 
     return Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth())! 
    } 
} 

使用它

override func viewDidLoad() { 
    super.viewDidLoad() 

    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "yyyy-MM-dd" 
    // Do any additional setup after loading the view, typically from a nib. 
    let datesArray = self.datesWith(dayNumber: 5, month: "06", year: "2017") 
    for currDate in datesArray { 
     debugPrint(dateFormatter.string(from: currDate)) 
    } 
} 

輸出

"2017-06-01" 
"2017-06-08" 
"2017-06-15" 
"2017-06-22" 
"2017-06-29" 

希望這會有幫助

相關問題