1

I am working on a project which lists different project locations, each project locations are associated with the different timezones. I am using FSCalendar for displaying date of the currently selected project in its timezone. I am not able to correctly display the date on FSCalendar View.

I want to show the date on FSCalendar view irrespective of time zone configured in the device. Meaning even if the user changes the timezone manually going to setting in the device, the this change should not affect in my project calendar. (eg: Automatic timezone will be selected usually in all devices)

To achieve this i have to configure FSCalendar with the selected project timezone.

I have tried converting the Date() to Project time zone using DateFormatter(). Tried DST calculations.

  func isDateInProjectTimeZone() -> Bool {
       if let projectTimeZone = AppUtils.timeZoneForProject() {
           let dateFormatter = DateFormatter()
           dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"

           var calendar = Calendar.current
           calendar.timeZone = projectTimeZone

           return calendar.isDateInToday(Date().dateInProjectTimeZone())
       }
       return false
  }
James Z
  • 12,209
  • 10
  • 24
  • 44
Ramprasad A
  • 221
  • 1
  • 13

2 Answers2

4

I was able to achieve my requirement with the below steps using FSCalendar, If anyone come across the same scenario the answer might be useful.

To change the FSCalendar timezone, please follow the below steps :

  • Create a custom class of FSCalendar and write the required initialiser methods as show below.

    import FSCalendar
    
    class XYZFSCalendar: FSCalendar {
    
       override init(frame: CGRect) {
          super.init(frame: frame)
       }
    
       required init?(coder aDecoder: NSCoder) {
    
           super.init(coder: aDecoder)
    
           if let projectTimeZone = <Set_Your_Time_Zone_Here> {
              self.setValue(projectTimeZone, forKey: "timeZone")
              self.perform(Selector(("invalidateDateTools")))
           }
       }
    }
    
  • The above code makes the FSCalendar independent of the system time

  • Assign the above custom calendar in the Storyboard as custom class of FSCalendar
  • If you assign any date to calendar

Eg:

import FSCalendar

class ViewController: UIVIewController {
    @IBOutlet weak var calendar: XYZFSCalendar!

    override func viewDidLoad() {
       super.viewDidLoad()

       //When you set the date below, the date will be displayed in the FSCalendar based on your custom timezone.
       calendar.select = Date()

    }
} 

This helped my scenario, hope it might also help the one in need. The timeZone may not change globally, but it works every time when you instantiate the class with your preset timeZone for FSCalendar.

Thank you.

Ramprasad A
  • 221
  • 1
  • 13
0

I've just had this same issues and resolved it by updating the project itself.

  1. Make the time zone public by deleting the property from FSCalendar.m and adding it to FSCalendar.h:
@property (copy, nonatomic) NSTimeZone *timeZone;
  1. Override the setter:
- (void)setTimeZone:(NSTimeZone *)timeZone
{
    if (![_timeZone isEqual:timeZone]) {
        _timeZone = timeZone.copy;
        [self invalidateDateTools];
    }
}
Leon
  • 3,614
  • 1
  • 33
  • 46