Can anyone help me with little problem? I am trying to get first month of given year.
func createEmptyYearChartData(year: Date, month: Int = 1, chartEntries: [ChartModel]) -> [ChartModel] {
var chartData = chartEntries
let calendar = Calendar.current
let currentYear = calendar.component(.year, from: year)
let currentMonth = month
var components = DateComponents()
components.year = currentYear
components.month = month
let date = calendar.date(from: components)
guard let date = calendar.date(from: components) else { return [] }
if month >= 12 {
components.month = 12
chartData.append(ChartModel(date: calendar.date(from: components)!, count: 0))
return chartData
} else {
if currentMonth == month{
chartData.append(ChartModel(date: date, count: 0))
chartData = createEmptyYearChartData(year: date, month: month + 1, chartEntries: chartData)
}
return chartData
}
}
This is the function I use to populate data by recursion, when the function fires I get the result of first date from past years 31. December, I tried to add one day also, in the function it switched to January 1. but the function output still gave me 31 December. The year passed in parameter is simply Date()
object.