0

How to display events on Fscalender?

This is my code to fetch events from api call.

func SetUpUIDashBoardCalenderdata()
{
    APIManager.sharedInstance.FetchParentDashboardCalenderDataFromURL(){(dashBoardCalenderJson)-> Void in
        let calenderVar = JSON(dashBoardCalenderJson)
        print("calenderVar----",calenderVar)
        let info = calenderVar["dates"].rawString()
        let jsonData = info?.data(using: .utf8)!
        let dictionary = try? JSONSerialization.jsonObject(with: jsonData!, options: [])  as! Array<Any>
        print("dictionary",dictionary)

    }
}
kishore
  • 3
  • 6
  • you must have to convert your date response into **yyyy-MM-dd** format.Try this link https://stackoverflow.com/questions/50020859/selection-color-changes-the-events-color-in-fscalendar/52170654#52170654 – Yash Bhikadiya Sep 17 '18 at 16:37

1 Answers1

0

You should implement FSCalendarDataSource protocol.

please take a look at my example :

let events = [Date]()

fileprivate lazy var dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy/MM/dd"
    return formatter
}()

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
    let dayFormatted = dateFormatter.string(from: date)
    var counter = 0
    for event in events{
        let day = dateFormatter.string(from: event)
        if dayFormatted == day{
            counter += 1
        }
    }
    return counter
}

or shorter :

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
    let dayFormatted = dateFormatter.string(from: date)
    return events.filter({ dateFormatter.string(from: $0) == dayFormatted }).count
}
Maor
  • 3,340
  • 3
  • 29
  • 38
  • i am getting dates in dictionary how to populates those dates on calender. – kishore Oct 11 '17 at 11:50
  • Can you add example of your dictionary? – Maor Oct 11 '17 at 11:59
  • This Is the response i am getting from the server.. "dates" : [ "Mon Oct 02 00:00:00 GMT+05:30 2017", "Wed Oct 04 00:00:00 GMT+05:30 2017", "Sat Oct 07 00:00:00 GMT+05:30 2017", "Sun Oct 08 00:00:00 GMT+05:30 2017", "Mon Oct 09 00:00:00 GMT+05:30 2017", "Wed Oct 18 00:00:00 GMT+05:30 2017", "Thu Oct 19 00:00:00 GMT+05:30 2017", "Fri Oct 20 00:00:00 GMT+05:30 2017", "Mon Oct 23 00:00:00 GMT+05:30 2017", "Tue Oct 24 00:00:00 GMT+05:30 2017", ], "status" : 1, "message" : "Details fetched successfully." } – kishore Oct 11 '17 at 12:03
  • @kishore This is not what you asked for in your question. You asked how to display events in `Fscalender`. btw `dictionary["dates"]` is your array of dates – Maor Oct 11 '17 at 15:01
  • sorry for the delay reply since 10 days i was in leave. – kishore Oct 17 '17 at 04:31
  • i used your methods but its not getting call. – kishore Oct 17 '17 at 04:31