[英]In iOS, how to save the current hour, minutes and seconds from current time into separate ints?
[英]Adding minutes to Current time from text input error (iOS)
當前有來自用戶的文本輸入,可以將其添加到當前時間,並以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時,Add的時間量不正確; 固定值是正確的,並且可以根據需要工作。
嘗試
int minutesToAdd = workingTime.text.intValue;
而不是int minutesToAdd = workingTime.text;
使用代碼,您可以將minutesToAdd設置為指針。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.