iPhone NSCalendar - Incorrect Calendar Drawn for users outside US
- by xoail
After trying to work on someone else's code, and so many different iterations, I am unable to understand what am I doing wrong with dates on a calendar month view. I am trying to present a calendar in month view fashion like calendar.app but unfortunately some users in countries like Australia, UK are reporting incorrect alignment of Days to Dates. For instance in Australia it displays 17th March as Friday. Any help is highly appreciated!
Here is the code sample I am using:
-(void)setCalendar
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:units fromDate:[NSDate date]];
self.currentDate = [gregorian dateFromComponents:comps];
NSInteger weekdayInteger=[comps weekday];
currentMonth=[comps month];
currentYear=[comps year];
NSString *dateString=[[NSString stringWithFormat:@"%@",currentDate] substringToIndex:10];
NSArray *array= [dateString componentsSeparatedByString:@"-"];
NSInteger currentDay=[[array objectAtIndex:2] intValue];
currentMonthConstant=currentMonth;
currentYearConstant=currentYear;
currentDayConstant=currentDay;
currentConstantDate=[NSString stringWithFormat:@"%d/%d/%d",currentDayConstant,currentMonthConstant,currentYearConstant];
currentFirstDay=((8+weekdayInteger-(currentDay%7))%7);
if(currentFirstDay==0)
currentFirstDay=7;
[gregorian release];
}
And this is how im populating the calendar:
-(void)fillCalender:(NSInteger)month weekStarts:(NSInteger)weekday year:(NSInteger)year
{
currentMonth=month;
currentYear=year;
currentFirstDay=weekday;
UIButton *temp;
NSInteger numberOfDays=[self getNumberofDays:month YearVlue:year];
currentLastDay=(currentFirstDay+numberOfDays-1)%7;
if(currentLastDay==0)
currentLastDay=7;
NSString *monthName=[self getMonth:month];
NSInteger grid=(weekday-1);
monthLabel.text=[NSString stringWithFormat:@"%@ %d",monthName,year];
for(int i=1;i<=numberOfDays;i++)
{
if([frontView isEqualToString:@"view1"])
temp=[arr_View2Buttons objectAtIndex:grid];
else
temp= [arr_View1Buttons objectAtIndex:grid];
[temp setBackgroundImage:nil forState:UIControlStateNormal];
[temp setUserInteractionEnabled:TRUE];
UILabel *aLbl = (UILabel *)[temp viewWithTag:123];
[aLbl setText:[NSString stringWithFormat:@"%d",i]];
// check for today
if(currentDayConstant == i && currentMonthConstant == currentMonth && currentYearConstant == currentYear)
{
[aLbl setTextColor:[UIColor blueColor]];
}
else
{
[aLbl setTextColor:[UIColor colorWithRed:69/255.0 green:2/255.0 blue:71/255.0 alpha:1.0]];
}
[temp setTag:i];
grid++;
if(grid==35)
grid=0;
}
}
And finally viewdidload:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setCalendar];
// set views
[self.view1 setFrame:CGRectMake(0,45,320,232)];
[self.view2 setFrame:CGRectMake(0,277,320,232)];
[self.view addSubview:self.view1];
[self.view addSubview:self.view2];
frontView = @"view2";
[self fillCalender:currentMonth weekStarts:currentFirstDay year:currentYear];
frontView = @"view1";
}