LouiOS
LouiOS

Reputation: 81

Why is my JTAppleCalendar 1 day ahead?

I'm using JTAppleCalendar and each months date is 1 day ahead than it should be. This is my code for the configuration:

func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {

    formatter.dateFormat = "yyyy MM dd"
    formatter.timeZone = Calendar.current.timeZone
    formatter.locale = Calendar.current.locale

    let currentYear = Calendar.current.component(.year, from: Date())
    let stringCurrentYear = String(currentYear)
    let nextYear = currentYear + 1
    let stringNextYear = String(nextYear)
    let currentMonth = Calendar.current.component(.month, from: Date())
    let stringCurrentMonth = String(currentMonth)

    let startDate = formatter.date(from: "\(stringCurrentYear) \(stringCurrentMonth) 01")!
    let endDate = formatter.date(from: "\(stringNextYear) 12 31")!

    let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate)

    return parameters

}

This is the current output:

enter image description here

The 1st of January 2018 should be a Monday however is appearing as a Tuesday.

Upvotes: 2

Views: 1245

Answers (2)

Joakim Danielson
Joakim Danielson

Reputation: 51892

The firstDayOfWeek parameter should only control what weekday you see in the leftmost column so I am doubtful this is the solution.

Given your use of nextYear as currentYear + 1 it looks more like what you see is January 2019 in your screenshot.

Rather than working with a formatter I would try to use DateComponents instead, something like this:

let calendar = Calendar.current
let date = Date()

let currentYear = Calendar.current.component(.year, from: Date())

var dateOneComponents = DateComponents()
dateOneComponents.year = currentYear
dateOneComponents.month = 1
dateOneComponents.day = 1
let dateOne = calendar.date(from: dateOneComponents)

var dateTwoComponents = DateComponents()
dateTwoComponents.year = currentYear
dateTwoComponents.month = 12
dateTwoComponents.day = 31
let dateTwo = calendar.date(from: dateTwoComponents)

Upvotes: 0

LouiOS
LouiOS

Reputation: 81

Found my own answer. I used the following code to fix it:

    let parameters = ConfigurationParameters(
        startDate: startDate,
        endDate: endDate,
        numberOfRows: 6,
        calendar: calendar,
        generateOutDates: .tillEndOfRow,
        firstDayOfWeek: .monday
    )

    return parameters

I think the issue was that by default the first day of the week was by default Sunday. So setting it to Monday resolved the problem.

Upvotes: 5

Related Questions