从文本输入错误(iOS)添加分钟到当前时间

时间:2014-02-06 22:44:47

标签: iphone objective-c ios7

目前有来自用户的文本输入,并将其添加到当前时间并在屏幕上显示为HH:mm格式的文本输入。

Current Code:-
NSString *strCurrentDate;
NSString *strNewDate;
NSDate *date = [NSDate date];
NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:@"hh:mm"];
strCurrentDate = [df stringFromDate:date];
NSLog(@"Current Time: %@",strCurrentDate);
int minutesToAdd = workingTime.text;
NSCalendar *calendar = [[NSCalendar 
alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setMinute:minutesToAdd];
NSDate *newDate= [calendar dateByAddingComponents:components toDate:date options:0];
[df setDateFormat:@"hh:mm"];
strNewDate = [df stringFromDate:newDate];
NSLog(@"workingtime is :%@",workingTime.text);
NSLog(@"New Date and Time: %@",strNewDate);
crewOutTime.text = strNewDate;

然而,当我将其更改为int minutesToAdd = 40时,添加不正确的时间量;一个固定的值,它是正确的,并按照需要工作。

1 个答案:

答案 0 :(得分:0)

尝试

int minutesToAdd = workingTime.text.intValue;
而不是     int minutesToAdd = workingTime.text;

使用您的代码,您将minutesToAdd设置为指针。