如果我有一个UIDatePicker,并且我希望将最小和最大日期范围设置为三十年前到三十年之间,我该如何设置它?
答案 0 :(得分:80)
未经测试,但你可能想要这样的东西。
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:30];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-30];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];
Swift 4.1的更新
let calendar = Calendar(identifier: .gregorian)
var comps = DateComponents()
comps.year = 30
let maxDate = calendar.date(byAdding: comps, to: Date())
comps.year = -30
let minDate = calendar.date(byAdding: comps, to: Date())
datePicker.maximumDate = maxDate
datePicker.minimumDate = minDate
答案 1 :(得分:18)
m_datePicker.maximumDate = [NSDate date];
您可以使用此行代码设置最大日期(今天)。
答案 2 :(得分:5)
我的快速4版@toddg是:
let currentDate = Date()
var dateComponents = DateComponents()
let calendar = Calendar.init(identifier: .gregorian)
dateComponents.year = -100
let minDate = calendar.date(byAdding: dateComponents, to: currentDate)
dateComponents.year = -13
let maxDate = calendar.date(byAdding: dateComponents, to: currentDate)
非常适合我!
答案 3 :(得分:3)
改编@warrenm对Swift的回答:
let currentDate = NSDate()
let dateComponents = NSDateComponents()
dateComponents.year = -13
let minDate = calendar.dateByAddingComponents(dateComponents, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))
dateComponents.year = 30
let maxDate = calendar.dateByAddingComponents(dateComponents, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))
dobPicker.maximumDate = maxDate
dobPicker.minimumDate = minDate
答案 4 :(得分:1)
请尝试使用此代码Ios8 +
td
答案 5 :(得分:1)
今天正在研究这个问题,并提出了一个扩展UIDatePicker的Swift 3解决方案。
extension UIDatePicker
{
/// set the date picker values and set min/max
/// - parameter date: Date to set the picker to
/// - parameter unit: (years, days, months, hours, minutes...)
/// - parameter deltaMinimum: minimum date delta in units
/// - parameter deltaMaximum: maximum date delta in units
/// - parameter animated: Whether or not to use animation for setting picker
func setDate(_ date:Date, unit:NSCalendar.Unit, deltaMinimum:Int, deltaMaximum:Int, animated:Bool)
{
setDate(date, animated: animated)
setMinMax(unit: unit, deltaMinimum: deltaMinimum, deltaMaximum: deltaMaximum)
}
/// set the min/max for the date picker (uses the pickers current date)
/// - parameter unit: (years, days, months, hours, minutes...)
/// - parameter deltaMinimum: minimum date delta in units
/// - parameter deltaMaximum: maximum date delta in units
func setMinMax(unit:NSCalendar.Unit, deltaMinimum:Int, deltaMaximum:Int)
{
if let gregorian = NSCalendar(calendarIdentifier:.gregorian)
{
if let minDate = gregorian.date(byAdding: unit, value: deltaMinimum, to: self.date)
{
minimumDate = minDate
}
if let maxDate = gregorian.date(byAdding: unit, value: deltaMaximum, to: self.date)
{
maximumDate = maxDate
}
}
}
}
setDate
方法将设置UIDatePicker
个实例的三个值(日期,最小值,最大值)。
setMinMax
仅设置最小值和最大值。最小值和最大值是使用选择器的当前日期计算的。
单位可以是以下值:
要设置加号和减去30年的日期,代码为:
var datePicker = UIDatePicker()
var date = Date()
datePicker.setDate(date, unit:.year, deltaMinimum:-30, deltaMaximum:30, animated:true)
答案 6 :(得分:1)
将最大日期设置为今天的Swift 4.2
datePicker.maximumDate = Date()
答案 7 :(得分:0)
将最小日期设置为今天的日期
@IBOutlet var datePicker: UIDatePicker!
datePicker.minimumDate = NSDate() as Date
这将使 DatePicker 自动滚动到当前日期,即使滚动到较旧的日期