我正在使用ActionSheetDatePicker
作为时间选择器,我将选择器中的模式更改为UIDatePickerModeCountDownTimer
以获取小时和分钟。它运行良好显示小时和分钟,但当我得到小时和分钟它返回我NSDate
初始化。
防爆。我选择了5分钟让我回归300,所以我得到了。
这是代码:
ActionSheetDatePicker *datePicker = [[ActionSheetDatePicker alloc] initWithTitle:@"Select a time" datePickerMode:UIDatePickerModeCountDownTimer selectedDate:self.selectedTime target:self action:@selector(ExtrTravelTimeWasSelected:element:) origin:sender];
datePicker.minuteInterval = minuteInterval;
[datePicker showActionSheetPicker];
和委托
- (void)ExtrTravelTimeWasSelected:(NSDate *)selectedTime element:(id)element {
self.selectedTime = selectedTime;
}
那么有没有办法将300( NSDate )转换为Hr和Min?
答案 0 :(得分:0)
如果我理解正确,你会在几秒钟内获得价值。尝试以下方法:
-(NSString *)getTimeStringFromSeconds:(double)seconds
{
NSDateComponentsFormatter *dcFormatter = [[NSDateComponentsFormatter alloc] init];
dcFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
dcFormatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute;
dcFormatter.unitsStyle =
NSDateComponentsFormatterUnitsStylePositional;
return [dcFormatter stringFromTimeInterval:seconds];
}
答案 1 :(得分:0)
试试这个,我已经更新了你的代码,
- (void)ExtrTravelTimeWasSelected:(NSDate *)selectedTime element:(id)element {
self.selectedTime = selectedTime;
// Split up the date components
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSCalendarUnitHour | NSCalendarUnitMinute
fromDate:selectedTime];
NSLog(@"h:mm::%zd:%2zd",[time hour],[time minute]);
}