Friday 1 February 2013

How To change day like calendar's next prev button..


#pragma mark view calnder Next Prev

-(IBAction)btnRigth_Clicked:(id)sender{
    NSLog(@"\n Right Button Clicked");
    [self NextDate:lblTitleDate.text];
}

-(IBAction)btnLeft_Clicked:(id)sender{
    NSLog(@"\n Left Button Clicked");
    [self PrevDate:lblTitleDate.text];
}

-(void)NextDate:(NSString *)strDate{
    
    NSDate *selectedDate = [[NSDate alloc]init];
    selectedDate = [self convertStringToDate:strDate];
    
    // start by retrieving day, weekday, month and year components for yourDate
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:selectedDate];
    NSInteger theDay = [todayComponents day];
    NSInteger theMonth = [todayComponents month];
    NSInteger theYear = [todayComponents year];
    
    // now build a NSDate object for yourDate using these components
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:theDay]; 
    [components setMonth:theMonth]; 
    [components setYear:theYear];
    NSDate *thisDate = [gregorian dateFromComponents:components];
    [components release];
    
    // now build a NSDate object for the next day
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setDay:1];
    NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
    [offsetComponents release];
    [gregorian release];
    lblTitleDate.text = [self convertDateToString:nextDate];
}

-(void)PrevDate:(NSString *)strDate{
    
    NSDate *selectedDate = [[NSDate alloc]init];
    selectedDate = [self convertStringToDate:strDate];
    // start by retrieving day, weekday, month and year components for yourDate
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:selectedDate];
    NSInteger theDay = [todayComponents day];
    NSInteger theMonth = [todayComponents month];
    NSInteger theYear = [todayComponents year];
    
    // now build a NSDate object for yourDate using these components
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:theDay]; 
    [components setMonth:theMonth]; 
    [components setYear:theYear];
    NSDate *thisDate = [gregorian dateFromComponents:components];
    [components release];
    
    // now build a NSDate object for the next day
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setDay:-1];
    NSDate *prevDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
    [offsetComponents release];
    [gregorian release];
    lblTitleDate.text = [self convertDateToString:prevDate];
}

-(NSDate *)convertStringToDate:(NSString *) date {
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
    NSDate *nowDate = [[[NSDate alloc] init] autorelease];
    [formatter setDateFormat:@"EEEE, dd MMM yyyy"];// set format here which format in string date
    date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
    nowDate = [formatter dateFromString:date];
    // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
    return nowDate;
}

-(NSString *)convertDateToString:(NSDate *) date {
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
    NSString *nowDate = [[[NSString alloc] init] autorelease];
    [formatter setDateFormat:@"EEEE, dd MMM yyyy"];// set format here which format in string date
    
    nowDate = [formatter stringFromDate:date];
    // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
    return nowDate;
}

No comments:

Post a Comment