嗨,在我的项目中,我有Textfield作为来自另一个是ex的延迟来自归档我有值为“18:30”(字符串)和延迟提交“18”(整数),现在我需要添加这些两个值,应该在另一个textfield中显示像“18:48”(字符串)的值。如果有人知道指导我,谢谢
答案 0 :(得分:7)
请参阅以下代码以查询您的问题。
NSString *str=@"18:30";
int addTime=18;
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSDate *dateInput=[formatter dateFromString:str];
dateInput=[dateInput dateByAddingTimeInterval:addTime*60];
NSString *dateOutPut=[formatter stringFromDate:dateInput];
NSLog(@"OutputDate= %@",dateOutPut);
控制台输出: 18时48分
答案 1 :(得分:2)
NSString *str=@"18:30";
int addTime=18;
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSDate *dateInput=[formatter dateFromString:str];
dateInput=[dateInput dateByAddingTimeInterval:addTime*60];
NSString *dateOutPut=[formatter stringFromDate:dateInput];
NSLog(@"OutputDate= %@",dateOutPut);
答案 2 :(得分:1)
//convert string object into NSDate object
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];
//Set System Timezone
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *TimeInDateFormat = [formatter dateFromString:@"12:45 AM"];
//Additing 01:00 Houes
NSTimeInterval secondsInOneHours = 1 * 60 * 60;
NSDate *dateOneHoursAhead = [TimeInDateFormat
dateByAddingTimeInterval:secondsInOneHours];
NSLog(@"after added dateOneHoursAhead: %@",dateOneHoursAhead);
abow代码输出是:
after added dateOneHoursAhead: 01:45 PM
现在尝试........
答案 3 :(得分:1)
这是快速版本。
let str: String = "10:30"
let addTime: Int = 30 //minutes
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
var dateInput: Date? = formatter.date(from: str)
dateInput = dateInput?.addingTimeInterval(TimeInterval(addTime*60))
endtime = formatter.string(from: dateInput!)
感谢@Andy Paul提供的客观c版代码。