Jtapplecalendar: How to show current month?

Created on 12 Jun 2017  ·  5Comments  ·  Source: patchthecode/JTAppleCalendar

In default month is January. How to show current month in start?
I do like this, but it is not security and previous months cropped.
Or how to jump to current month?

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

    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy MM dd"
    formatter.timeZone = Calendar.current.timeZone
    formatter.locale = Calendar.current.locale

    let date = Date()
    let calendar = Calendar.current
    let components = calendar.dateComponents([.year, .month], from: date)
    let formatter2 = DateFormatter()
    formatter2.dateFormat = "yyyy MM"
    let year =  components.year
    let month = components.month

    let startDate = formatter2.date(from: "\(year!) \(month!)")
    let endDate = formatter.date(from: "2017 12 31")

    let parameters = ConfigurationParameters(startDate: startDate!, endDate: endDate!, firstDayOfWeek: .monday)
    return parameters
}
question

Most helpful comment

you can scroll to the date you want.
So in your viewDidLoad function, you can put this

calendarView.scrollToDate( theDateYouWantToScrollTo )

That function has many options. You can check them out. It even includes option for non-animated scrolling to date.

Cheers 🍻

All 5 comments

you can scroll to the date you want.
So in your viewDidLoad function, you can put this

calendarView.scrollToDate( theDateYouWantToScrollTo )

That function has many options. You can check them out. It even includes option for non-animated scrolling to date.

Cheers 🍻

Didn't want to open a new case for this as it's related. Should I in the future?

I'm trying to figure out how to set the days correctly when called scrollToDate(date). When the function is called, it seems to set the date to be under the column of Monday regardless of what day is called. For example, if I scroll to date 2017 02 10 which is a Wednesday, the column will still appear under Monday until something is selected.

@lukejones1 where your date lands after a scroll depends on your scrolling mode.
https://patchthecode.github.io/CalendarScrolling/

I fixed this like this. I hope it helps someone. There is probably a better way (less code) but this works fine for me

This is a Delegate that tells you the current selected month whenever you swipe/change the month as an event. I needed this for a API call

func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
        let helper = NSCalendar.init(calendarIdentifier: NSCalendar.Identifier.gregorian)
        let this_date = calendar.visibleDates().monthDates.first(where: { (helper?.component(NSCalendar.Unit.day, from: $0.date))! == 1 })?.date as Any
        let localDate = Date(timeInterval: TimeInterval(Calendar.current.timeZone.secondsFromGMT()), since: this_date as! Date)
        print("DATE IS \(localDate)")
}

@Coners Thanks for this, it helped me - although there's a small bug in your code - it doesn't account for daylight saving time, which resulted for me in a duplicate month header (in the month following the one when we change time). I fixed it by just changing the way seconds from GMT are calculated:

let localDate = Date(timeInterval: TimeInterval(Calendar.current.timeZone.secondsFromGMT(for: this_date)), since: this_date)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

PedroAnibarro1 picture PedroAnibarro1  ·  5Comments

blinkmeoff picture blinkmeoff  ·  3Comments

Kevinw14 picture Kevinw14  ·  3Comments

arn00s picture arn00s  ·  3Comments

dbmrq picture dbmrq  ·  5Comments