I have a label and two arrow buttons. When I push on the left arrow I want to show one day less than that date at the moment. And when I push on the right arrow a day should be added.
At the moment this is my code.
- (IBAction)addDay:(id)sender {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSString *dateNow = _lblDate.text;
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE. d MMMM YYYY"];
NSDate *date = [dateFormat dateFromString:dateNow];
date = [theCalendar dateByAddingComponents:dayComponent toDate:date options:0];
NSString *dateString = [dateFormat stringFromDate:date];
_lblDate.text = dateString;
}
- (IBAction)deleteDay:(id)sender {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSString *dateNow = _lblDate.text;
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE. d MMMM YYYY"];
NSDate *date = [dateFormat dateFromString:dateNow];
date = [theCalendar dateByAddingComponents:dayComponent toDate:date options:0];
NSString *dateString = [dateFormat stringFromDate:date];
_lblDate.text = dateString;
}
But for some reason or another the dates are wrong. There is no pattern when I'm adding and deleting dates.
Can anybody help me ?
Kind regards
Your code for addition is correct. However relying on string to date conversion is creating problems. I'll advise you to take a NSDate
object as instance variable and then update this instance variable on going to next or previous day. After updating value you can use dateformatter to retrieve string from this date. Right now when you use your label's text to generate date, it creates problems.
So that's how you can do it
// currentDate is my instance variable.
currentDate = [[NSDate date] retain];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"EE. d MMMM YYYY"];
dateLabel.text = [dateFormat stringFromDate:currentDate];
And that's how your nextdayfunctionwill look Like
(IBAction)nextDay:(id)sender {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"EE. d MMMM YYYY"];
currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain];
NSString *dateString = [dateFormat stringFromDate:currentDate];
dateLabel.text = dateString;
}
And here is previous day
enter code here
(IBAction)previousDay:(id)sender {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"EE. d MMMM YYYY"];
currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain];
NSString *dateString = [dateFormat stringFromDate:currentDate];
dateLabel.text = dateString;
}