I want to get list of month names between two dates
Date1 : 1/1/2016
Date2 : 1/4/2017
Result should be :
January 2016
February 2016
March 2016
April 2016 To April 2017
I want to get list of month names between two dates
Date1 : 1/1/2016
Date2 : 1/4/2017
Result should be :
January 2016
February 2016
March 2016
April 2016 To April 2017
Use below code to get a list of years between two dates.
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
}
}
//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
}
}
Result:
for dates between "01/01/2017" to "04/01/2017"
monthsStringArray = ["January", "February", "March", "April"]
monthsIntArray = [1, 2, 3, 4]
monthsWithyear = ["January 2017", "February 2017", "March 2017", "April 2017"]
for dates between "08/01/2016" to "04/01/2018"
monthsWithyear = ["August 2016", "September 2016", "October 2016", "November 2016", "December 2016", "January 2017", "February 2017", "March 2017", "April 2017", "May 2017", "June 2017", "July 2017", "August 2017", "September 2017", "October 2017", "November 2017", "December 2017", "January 2018", "February 2018", "March 2018", "April 2018"]
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"]
This is a solution with the method enumerateDates(startingAfter:matching:matchingPolicy:)
of Calendar
to enumerate dates very efficiently based on 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)
Making the 'Krunal' answer a little more clean:
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
}
}