web-dev-qa-db-ja.com

UIDatePickerの最小および最大日付

日付ピッカーから最小日付と最大日付を取得したいのですが、最小日付は現在の日付の「-18」で、最大日付は現在の日付の「-100」にする必要があります。

現在の年が2018年だとすると、最小日付2000と最大日付1918が必要です。

私がこれまでにしたことは:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:[NSDate date]];

NSInteger year = [components year];

int mindt = year - 18;

int maxdt = year -100;

   // NSDate * MinDate = [components year] - 18;

   // NSDate * MaxDate =  [components year] - 100;

   // self.datePicker.minimumDate = MinDate;

   // self.datePicker.maximumDate = MaxDate;

しかし、この整数を日付形式に変換することはできません。

42
NSException

これを試して:

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;

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
67
tilo

Swiftバージョン。ユーザーの負の値(-)は減算に、正の値はadd日付コンポーネントになります。

1。最小日付の設定

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

2。最大日付の設定

 yourDatePicker.maximumDate = Calendar.current.date(byAdding: .year, value: 1, to: Date())
22
Rahul Panzade

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)")
14
King-Wizard

これは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
13
cldrr

Swift 5.1

extension UIDatePicker {
    func set18YearValidation() {
        let currentDate: Date = Date()
        var calendar: Calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        calendar.timeZone = TimeZone(identifier: "UTC")!
        var components: DateComponents = DateComponents()
        components.calendar = calendar
        components.year = -18
        let maxDate: Date = calendar.date(byAdding: components, to: currentDate)!
        components.year = -150
        let minDate: Date = calendar.date(byAdding: components, to: currentDate)!
        self.minimumDate = minDate
        self.maximumDate = maxDate
    } }
13
swift2geek

Swift 3.

 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
7
Petro Nepyivoda

** UIDatePickerの最小および最大日付:#Swift

**以下の簡単なコードを使用して、2つの日付値の間で最小日付と最大日付を選択する場合:**

例:最小日付:2017年1月4日および最大日付:2018年3月31日

let calendar = Calendar.current
var minDateComponent = calendar.dateComponents([.day,.month,.year], from: Date())
minDateComponent.day = 01
minDateComponent.month = 04
minDateComponent.year = 2017

let minDate = calendar.date(from: minDateComponent)
print(" min date : \(String(describing: minDate))")

var maxDateComponent = calendar.dateComponents([.day,.month,.year], from: Date())
maxDateComponent.day = 0
maxDateComponent.month = 03 + 1
maxDateComponent.year = 2018

let maxDate = calendar.date(from: maxDateComponent)
print("max date : \(String(describing: maxDate))")

picker.minimumDate = minDate! as Date
picker.maximumDate =  maxDate! as Date
3
Kiran jadhav

Swift 4.1

Calendar.Componentに基づいて、すべての種類の制限の拡張機能を作成しました

extension UIDatePicker {

    func setLimit(forCalendarComponent component:Calendar.Component, minimumUnit min: Int, maximumUnit max: Int) {

        let currentDate: Date = Date()
        var calendar: Calendar = Calendar(identifier: Calendar.Identifier.gregorian)

        guard let timeZone = TimeZone(identifier: "UTC") else { return }
        calendar.timeZone = timeZone

        var components: DateComponents = DateComponents()
        components.calendar = calendar

        components.setValue(-min, for: component)
        if let maxDate: Date = calendar.date(byAdding: components, to: currentDate) {
            self.maximumDate = maxDate
        }

        components.setValue(-max, for: component)
        if let minDate: Date = calendar.date(byAdding: components, to: currentDate) {
            self.minimumDate = minDate
        }
    }

}

これは次のように使用できます。

self.datePicker.setLimit(forCalendarComponent: .year, minimumUnit: 13, maximumUnit: 100)
2
g212gs

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

最小の日付は1900年1月1日です。すべての人に役立つことを願っています。:)

In Swift 2.0以降

    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
2
Vinay Podili

Swift 4.

extension UIDatePicker { func set18YearValidation() { let currentDate: Date = Date() var calendar: Calendar = Calendar(identifier: Calendar.Identifier.gregorian) calendar.timeZone = TimeZone(identifier: "UTC")! var components: DateComponents = DateComponents() components.calendar = calendar components.year = -18 let maxDate: Date = calendar.date(byAdding: components, to: currentDate)! components.year = -150 let minDate: Date = calendar.date(byAdding: components, to: currentDate)! self.minimumDate = minDate self.maximumDate = maxDate } }

1
PinkeshGjr

別のケース:

生年月日を日付ピッカーとして表示したいとします。Dobの範囲は1980年1月1日から2000年1月1日までです。

@IBOutlet var datePicker: UIDatePicker!

override func viewDidLoad() {
        super.viewDidLoad()        
        datePicker.datePickerMode = UIDatePickerMode.date       


        // 01/01/1980 to 01/01/2000

        let calendar = Calendar.current
        var minDateComponent = calendar.dateComponents([.day,.month,.year], from: Date())
        minDateComponent.day = 01
        minDateComponent.month = 01
        minDateComponent.year = 1980

        let minDate = calendar.date(from: minDateComponent)
        print(" min date : \(minDate)")

        var maxDateComponent = calendar.dateComponents([.day,.month,.year], from: Date())
       maxDateComponent.day = 01
        maxDateComponent.month = 01
        maxDateComponent.year = 2000

        let maxDate = calendar.date(from: maxDateComponent)
        print("max date : \(maxDate)")

        self.datePicker.minimumDate = minDate! as Date
        self.datePicker.maximumDate =  maxDate! as Date

}
0
A.G