我如何以下面的格式打印给定年份的前五个月的日期

How can i print the dates of the first five month of a given year in the format below

我想使用以下格式打印给定年份的前 5 个月的日期。

2019-05, 2019-04, 2019-03, 2019-02, 2019-01

我正在使用 for 循环来实现这个

    var  first5monthIn2019 : [String]{
    var dates : [String] = []
    for i in 01...05 {
        dates.append("2019-0\(i)")
    }
      return dates
    }

是否有任何可接受的方法使用 NSdate 来实现上述目标。

这是一个使用 DateComponents 的简单解决方案

for month in 1...5 {
    if let date = DateComponents(calendar: Calendar.current, year: 2019, month: month, day: 1).date {
        print(date)
    }
}