Question

I'm new to iOS development (coming from Android development) and I need some help. I'm using Vurig's library to display a custom calendar control and mark multiple dates in the calendar.

The issue is I would like to mark Hijri dates rather than gregorian. How can I do that?

So far I've converted the Hijri date to gregorian to mark a date on the calendar, but it marks only one date whereas I would like to mark multiple dates in the calendar months.

The code so far:

-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated {
    NSArray *dates;


    NSDate *date = [self addDates:calendarView andDay:6 andMonth:10];

//    if (month==8){
//       dates = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],[NSNumber numberWithInt:25], nil];
//    }
//    if (month==9) {
//       dates = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:5], nil];
//       
//    }

    //if (month==[[NSDate date] month]) {

    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:( NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit) fromDate:date];

    int gmonth = [dateComponents month];
    if (month == gmonth) {
        dates = [NSArray arrayWithObjects:date, nil];
    }

    [calendarView markDates:dates];


}

-(void)calendarView:(VRGCalendarView *)calendarView dateSelected:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; [dateFormatter setDateFormat:@"dd MMMM"];

    NSLog(@"Selected date = %@",[dateFormatter stringFromDate:date]);

    //NSLog(@"Selected date = %@",date);
}


-(NSDate *)addDates:(VRGCalendarView *)calendarView andDay:(int)day andMonth:(int)month{
    // Then create an Islamic calendar
    NSCalendar *hijriCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];

    // And grab those date components for the same date
    NSDateComponents *hijriComponents = [[NSDateComponents alloc] init];

    hijriComponents.day = day;
    hijriComponents.month = month;
    hijriComponents.year = 1434;

    NSDate *hdate = [hijriCalendar dateFromComponents:hijriComponents];


    // Create a Gregorian Calendar
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    // Set up components of a Gregorian date
    NSDateComponents *gregorianComponents = [gregorianCalendar components:(NSDayCalendarUnit |
                                                                           NSMonthCalendarUnit |
                                                                           NSYearCalendarUnit)
                                                                 fromDate:hdate];
    // Create the date
    NSDate *date = [gregorianCalendar dateFromComponents:gregorianComponents];

    NSLog(@"[In Hijri calendar ->] Date is %@", date);
    return date;

}
Was it helpful?

Solution

All you need to do is pass an array of NSDates to the markDates: method. You don't need to convert to Gregorian first as an NSDate is simply a time stamp and is independent of calendars, time zones, and so forth.

I downloaded the VURIG Calendar sample project and in VRGViewController replaced the calendarView:switchedToMonth:targetHeight:animated: method with the following, which should illustrate exactly what you need:

-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:6];
    [components setMonth:10];
    [components setYear:1434];
    NSDate *date1 = [calendar dateFromComponents:components];

    [components setDay:10];
    NSDate *date2 = [calendar dateFromComponents:components];

    [calendarView markDates:@[date1,date2]];
}

This marked Aug 12 and Aug 16 on the calendar, and while I'm not familiar with the Hijri calendar from what I can tell that's accurate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top