我有一个标签和两个箭头按钮。当我按下左箭头时,我想显示比该日期少一天。当我按下右箭头时,应该添加一天。
目前这是我的代码。
- (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;
}
但由于某种原因,日期是错误的。添加和删除日期时没有模式。
有谁能够帮助我 ?
亲切的问候