Minimum and maximum date in UIDatePicker

前端 未结 11 1237
不知归路
不知归路 2020-11-30 00:34

I want to get the minimum and maximum date from a date picker, but minimum date should be \"- 18\" of the current date and the maximum date should be \"- 100\" of current da

相关标签:
11条回答
  • 2020-11-30 00:53

    This is how it would look like in 2017 (Swift 3)

        var components = DateComponents()
        components.year = -100
        let minDate = Calendar.current.date(byAdding: components, to: Date())
    
        components.year = -18
        let maxDate = Calendar.current.date(byAdding: components, to: Date())
    
        datePicker.minimumDate = minDate
        datePicker.maximumDate = maxDate
    
    0 讨论(0)
  • 2020-11-30 00:54

    Try this:

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:-18];
    NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
    [comps setYear:-150];
    NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
    [comps release];
    
    self.datePicker.minimumDate = minDate;
    self.datePicker.maximumDate = maxDate;
    

    It may be easily translated to Swift 4.2:

        let calendar = Calendar(identifier: .gregorian)
    
        let currentDate = Date()
        var components = DateComponents()
        components.calendar = calendar
    
        components.year = -18
        components.month = 12
        let maxDate = calendar.date(byAdding: components, to: currentDate)!
    
        components.year = -150
        let minDate = calendar.date(byAdding: components, to: currentDate)!
    
        picker.minimumDate = minDate
        picker.maximumDate = maxDate
    
    0 讨论(0)
  • 2020-11-30 00:57

    Look at this for Swift version. User negative value (-) to subtract and positive value to add date component.

    1. Set minimum date

     yourDatePicker.minimumDate = Calendar.current.date(byAdding: .year, value: -1, to: Date())
    

    2. Set maximum date

     yourDatePicker.maximumDate = Calendar.current.date(byAdding: .year, value: 1, to: Date())
    
    0 讨论(0)
  • 2020-11-30 01:01

    In Swift 2.1:

        let currentDate: NSDate = NSDate()
    
        let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        // let calendar: NSCalendar = NSCalendar.currentCalendar()
        calendar.timeZone = NSTimeZone(name: "UTC")!
    
        let components: NSDateComponents = NSDateComponents()
        components.calendar = calendar
    
        components.year = -18
        let minDate: NSDate = calendar.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
    
        components.year = -150
        let maxDate: NSDate = calendar.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
    
        self.dateOfBirthUIDatePicker.minimumDate = minDate
        self.dateOfBirthUIDatePicker.maximumDate = maxDate
    
        print("minDate: \(minDate)")
        print("maxDate: \(maxDate)")
    
    0 讨论(0)
  • 2020-11-30 01:02

    Swift 3.0

     let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
        let currentDate: Date = Date()
        let components: NSDateComponents = NSDateComponents()
    
        components.year = -150
        let minDate: Date = gregorian.date(byAdding: components as DateComponents, to: currentDate, options: NSCalendar.Options(rawValue: 0))!
    
        components.year = -16
        let maxDate: Date = gregorian.date(byAdding: components as DateComponents, to: currentDate, options: NSCalendar.Options(rawValue: 0))!
    
        self.datepicker.minimumDate = minDate
        self.datepicker.maximumDate = maxDate
    
    0 讨论(0)
  • 2020-11-30 01:03

    In Objective C, NSDateComponents *comps = [[NSDateComponents alloc] init];

    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
    
    NSInteger year = components.year;
    NSInteger month = components.month;
    NSInteger day = components.day;
    NSInteger minimumYear = year - 1900;//Given some year here for example
    NSInteger minimumMonth = month - 1;
    NSInteger minimumDay = day - 1;
    [comps setYear:-minimumYear];
    [comps setMonth:-minimumMonth];
    [comps setDay:-minimumDay];
    NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    
    [datePicker setMinimumDate:minDate];
    [datePicker setMaximumDate:[NSDate date]];
    

    The minimum date would be January 01 1900. I hope it works for you all.. :)

    In Swift 2.0 or later

        let calendar = NSCalendar(calendarIdentifier:NSCalendarIdentifierGregorian);
    
        let todayDate = NSDate()
    
        let components = calendar?.components([NSCalendarUnit.Year,NSCalendarUnit.Month,NSCalendarUnit.Day], fromDate: todayDate)
        let minimumYear = (components?.year)! - 1900
        let minimumMonth = (components?.month)! - 1
        let minimumDay = (components?.day)! - 1
    
        let comps = NSDateComponents();
        comps.year = -minimumYear
        comps.month = -minimumMonth
        comps.day = -minimumDay
    
        let minDate = calendar?.dateByAddingComponents(comps, toDate: todayDate, options: NSCalendarOptions.init(rawValue: 0))
    
        datePicker.minimumDate = minDate
        datePicker.maximumDate = datePicker.date
    
    0 讨论(0)
提交回复
热议问题