UIDatePicker,根据今天设定最大和最小的日期

时间:2021-04-06 16:00:52

If I have a UIDatePicker, and I wish to set the minimum and maximum date range to be between thirty years ago and thirty years in the future, how would I set that up?

如果我有一个UIDatePicker,我想设定最小和最大的日期范围在30年前到30年后,我该如何设定?

6 个解决方案

#1


73  

Not tested, but you probably want something like this.

没有经过测试,但是您可能需要这样的东西。

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];

#2


17  

m_datePicker.maximumDate = [NSDate date];

You can use this line of code to set max date (Today).

您可以使用这一行代码来设置最大日期(今天)。

#3


3  

Adapting @warrenm's answer to Swift:

改编@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  

Please Try this code Ios8+

请试试这个代码Ios8+。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
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];

#5


1  

Was looking at this issue today and came up with a solution in Swift 3 that extends UIDatePicker.

今天我们关注了这个问题,并在Swift 3中提出了扩展UIDatePicker的解决方案。

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
            }
        }
    }
}

The setDate method will set three values (date, minimum, maximum) of the UIDatePicker instance.

setDate方法将设置UIDatePicker实例的三个值(日期、最小值和最大值)。

setMinMax only sets the minimum and maximum. Minimum and maximum are calculated using the picker's current date.

setMinMax只设置最小值和最大值。最小值和最大值是用拾取器当前日期计算的。

Unit can be the following values:

单位可为:

  • .year
  • .month
  • .month
  • .day
  • 要擦防晒霜
  • .hour
  • .hour
  • .minute
  • .minute
  • .second
  • 接着

To set the date with plus and minus thirty years, the code would be:

将日期设为±30年,代码为:

var datePicker = UIDatePicker()
var date = Date()

datePicker.setDate(date, unit:.year, deltaMinimum:-30, deltaMaximum:30, animated:true)

#6


1  

My swift 4 version of @toddg is :

我的swift 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)

Works great for me !

对我来说太棒了!

#1


73  

Not tested, but you probably want something like this.

没有经过测试,但是您可能需要这样的东西。

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];

#2


17  

m_datePicker.maximumDate = [NSDate date];

You can use this line of code to set max date (Today).

您可以使用这一行代码来设置最大日期(今天)。

#3


3  

Adapting @warrenm's answer to Swift:

改编@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  

Please Try this code Ios8+

请试试这个代码Ios8+。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
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];

#5


1  

Was looking at this issue today and came up with a solution in Swift 3 that extends UIDatePicker.

今天我们关注了这个问题,并在Swift 3中提出了扩展UIDatePicker的解决方案。

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
            }
        }
    }
}

The setDate method will set three values (date, minimum, maximum) of the UIDatePicker instance.

setDate方法将设置UIDatePicker实例的三个值(日期、最小值和最大值)。

setMinMax only sets the minimum and maximum. Minimum and maximum are calculated using the picker's current date.

setMinMax只设置最小值和最大值。最小值和最大值是用拾取器当前日期计算的。

Unit can be the following values:

单位可为:

  • .year
  • .month
  • .month
  • .day
  • 要擦防晒霜
  • .hour
  • .hour
  • .minute
  • .minute
  • .second
  • 接着

To set the date with plus and minus thirty years, the code would be:

将日期设为±30年,代码为:

var datePicker = UIDatePicker()
var date = Date()

datePicker.setDate(date, unit:.year, deltaMinimum:-30, deltaMaximum:30, animated:true)

#6


1  

My swift 4 version of @toddg is :

我的swift 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)

Works great for me !

对我来说太棒了!