如何获取包含年份的月份名称和两个日期之间的年份列表

时间:2017-06-13 13:15:19

标签: ios xcode date swift3 calendar

我想获得两个日期之间的月份名称列表

  

日期1:2016年1月1日

     

日期2:1/4/2017

Result should be : 
    January 2016 
    February 2016
    March 2016
    April 2016 To  April 2017

5 个答案:

答案 0 :(得分:3)

 //let startDateString:String  = "01/01/2017"
 //let endDateString:String = "04/01/2017"

 let startDateString:String  = "08/01/2016"
 let endDateString:String = "04/01/2018"

 let dateFormtter = DateFormatter()
 dateFormtter.dateFormat = "MM/dd/yyyy"

 let startDate = dateFormtter.date(from: startDateString)
 let endDate = dateFormtter.date(from: endDateString)

 var monthsStringArray = [String]()
 var monthsIntArray = [Int]()
 var monthsWithyear = [String]()
 dateFormtter.dateFormat = "MM"

 if let startYear: Int = startDate?.year(), let endYear = endDate?.year() {

        if let startMonth: Int = startDate?.month(), let endMonth: Int = endDate?.month() {
            for i in startYear...endYear {
                for j in (i == startYear ? startMonth : 1)...(i < endYear ? 12 : endMonth) {
                    let monthTitle = dateFormtter.monthSymbols[j - 1]
                    monthsStringArray.append(monthTitle)
                    monthsIntArray.append(j)

                    let monthWithYear = "\(monthTitle) \(i)"
                    monthsWithyear.append(monthWithYear)
                }
            }
        }

 }

    print(monthsStringArray)
    print(monthsIntArray)
    print(monthsWithyear)


//-------
extension Date {

    func month() -> Int {
        let month = Calendar.current.component(.month, from: self)
        return month
    }

    func year() -> Int {
      let year = Calendar.current.component(.year, from: self)
      return year
   }
}
  

<强>结果:   
日期为&#34; 01/01/2017&#34;到&#34; 04/01/2017&#34;   
monthsStringArray = [&#34; 1月&#34;,&#34; 2月&#34;,&#34; 3月&#34;,&#34; 4月&#34;]   
monthsIntArray = [1,2,3,4]   
月份= [&#34; 2017年1月&#34;,&#34; 2017年2月&#34;,&#34; 2017年3月&#34;,&#34; 2017年4月&#34;]   

适用于&#34; 08/01/2016&#34;到&#34; 04/01/201&#34;   
月份= [&#34; 2016年8月&#34;,&#34; 2016年9月&#34;,&#34; 2016年10月&#34;,&#34; 2016年11月&#34;,&#34 ; 2016年12月&#34;,&#34; 2017年1月&#34;,&#34; 2017年2月&#34;,&#34; 2017年3月&#34;,&#34; 2017年4月&#34;,&#34 ; 2017年5月&#34;,&#34; 2017年6月&#34;,&#34; 2017年7月&#34;,&#34; 2017年8月&#34;,&#34; 2017年9月&#34;,&#34 ; 2017年10月&#34;,&#34; 2017年11月&#34;,&#34; 2017年12月&#34;,&#34; 2018年1月&#34;,&#34; 2018年2月&#34;,&#34 ; 2018年3月&#34;,&#34; 2018年4月&#34;]

答案 1 :(得分:1)

使用以下代码获取两个日期之间的年份列表。

func getYear(){
    let startDateString:String  = "05/21/2014"
    let endDateString:String = "05/21/2017"

    let dateFormtter = DateFormatter()
    dateFormtter.dateFormat = "MM/dd/yyyy"

    let startDate = dateFormtter.date(from: startDateString)
    let endDate = dateFormtter.date(from: endDateString)

    var arrYears = [String]()
    dateFormtter.dateFormat = "MM"

    if let startYear: Int = startDate?.year(), let endYear = endDate?.year() {
        for i in startYear...endYear {
            let monthWithYear = "\(i)"
            arrYears.append(monthWithYear)
        }
    }
}

extension Date {
   func Year() -> Int {
        let year = Calendar.current.component(.year, from: self)
        return year
    }
}

答案 2 :(得分:0)

    var monthsList = [String]()
    let fmt = DateFormatter()
    let startingMonthNumber:Int  = 0
    let endingMonthNumber:Int  = 3

    for i in startingMonthNumber..<endingMonthNumber {
        let monthName = fmt.monthSymbols[i]
        monthsList.append(monthName)
    }

    print(monthsList) //["January", "February", "March"]

答案 3 :(得分:0)

这是enumerateDates(startingAfter:matching:matchingPolicy:) Calendar方法的解决方案,可根据DateComponents非常有效地枚举日期

let calendar = Calendar.current
let dateFormtter = DateFormatter()
dateFormtter.dateFormat = "dd/MM/yyyy"

// create the start and end dates from the strings
let startDate = dateFormtter.date(from: "1/1/2016")!
let endDate = dateFormtter.date(from: "1/4/2017")!
// set the date format to the destination format
dateFormtter.dateFormat = "MMMM yyyy"

// Create the matching conponent : day 1 of each month
let components = DateComponents(day: 1)
// The variable result will contain the array of month names,
var result = [dateFormtter.string(from: startDate)]

// this particular method of Calendar enumerates each date matching the components
calendar.enumerateDates(startingAfter: startDate, matching: components, matchingPolicy: .nextTime) { (date, strict, stop) in
    result.append(dateFormtter.string(from: date!))
    // exit the enumeration if the end date is reached
    if date! >= endDate { stop = true }
}

print(result)

答案 4 :(得分:0)

让'Krunal'回答更清洁:

enum monthYearArrayType {
    case monthsStringArray
    case monthsIntArray
    case monthsWithyear
    case monthsWithyearExceptCurrent
}

func monthsBetweenDates(startDate: Date?, endDate: Date?, displayType: monthYearArrayType) -> Array<Any> {
    let dateFormtter = DateFormatter()

    var monthsStringArray = [String]()
    var monthsIntArray = [Int]()
    var monthsWithyear = [String]()
    var monthsWithyearExceptCurrent = [String]()
    dateFormtter.dateFormat = "MM"

    if let startYear: Int = startDate?.year(), let endYear = endDate?.year() {

        if let startMonth: Int = startDate?.month(), let endMonth: Int = endDate?.month() {
            for i in startYear...endYear {
                for j in (i == startYear ? startMonth : 1)...(i < endYear ? 12 : endMonth) {
                    let monthTitle = dateFormtter.monthSymbols[j - 1]
                    monthsStringArray.append(monthTitle)
                    monthsIntArray.append(j)

                    let monthWithYear = "\(monthTitle) \(i)"
                    monthsWithyear.append(monthWithYear)

                    if(i == Date().year()) {
                        monthsWithyearExceptCurrent.append(monthTitle)
                    } else {
                        monthsWithyearExceptCurrent.append(monthWithYear)
                    }
                }
            }
        }

    }

    switch displayType {
    case .monthsWithyear:
        return monthsWithyear
    case .monthsWithyearExceptCurrent:
        return monthsWithyearExceptCurrent
    case .monthsStringArray:
        return monthsStringArray
    case .monthsIntArray:
        return monthsIntArray
    }
}


extension Date {
    func month() -> Int {
        let month = Calendar.current.component(.month, from: self)
        return month
    }

    func year() -> Int {
        let year = Calendar.current.component(.year, from: self)
        return year
    }
}