I have created a custom class for my dates and need move dates forward and backward via button . Here is my code which shows Today date :
- (NSString *) showToday {
NSCalendar *myCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setCalendar:myCal];
offsetComponents = [[NSDateComponents alloc] init];
//shows today
[offsetComponents setDay:0];
NSDate *nextDate = [myCal dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0];
[dateFormatter setDateFormat:@"d"];
NSString *currDay = [dateFormatter stringFromDate:nextDate];
[NSString stringWithFormat:@"%@",currDay];
[myCal release];
[dateFormatter release];
return currDay;
}
on my viewController :
customClass = [[customClass alloc]init];
day.text = [cal showToday];
so if I need to move forward a date I just change this line code to :
//show tomorrow
[offsetComponents setDay:1];
so How can I dynamically change this line and change dates via button ?
You can declare an
intfor youroffsetComponentsvariable. then :