简体   繁体   English

比较当前日期与 Swift 中的 DatePicker 值

[英]Compare Current Date to DatePicker Value in Swift

In my app a user can select whether or not they are under 18 or over 18 years of age.在我的应用程序中,用户可以选择他们是 18 岁以下还是 18 岁以上。 The user enters their Date of Birth using a Date Picker.用户使用日期选择器输入他们的出生日期。 I need to make a function that will compare the current date in MM/DD/YYYY format to the DatePicker date to see if the user's entered age is over 18.我需要创建一个函数,将 MM/DD/YYYY 格式的当前日期与 DatePicker 日期进行比较,以查看用户输入的年龄是否超过 18 岁。

My current function for setting the DatePicker text to the associated textfield is:我当前将 DatePicker 文本设置为关联文本字段的功能是:

func updatePicker() {
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM/dd/yyyy"
    dob.text = dateFormatter.stringFromDate(datePickerView.date)
}

When the user tries to go the next page, the form is validated which is when I need to compare the dates and display the alert if they're under 18.当用户尝试进入下一页时,表单会得到验证,此时我需要比较日期并在 18 岁以下时显示警报。

Just not sure where to start with date string evaluation.只是不确定从哪里开始日期字符串评估。

Use this function to compare date 使用此功能比较日期

func compareDate(date1: NSDate!, toDate date2: NSDate!, toUnitGranularity unit: NSCalendarUnit) -> NSComparisonResult

see this question 看到这个问题

don't convert the date to text and later convert the text to the date again. 不要将日期转换为文本,然后再将文本转换为日期。 just keep the date from the datePicker around till you need it for the validation. 只需保留datePicker中的日期,直到您需要它进行验证。

have a member variable var selectedDate : NSDate? 有一个成员变量var selectedDate : NSDate?

then later to check if older than 18 just do 然后检查是否超过18岁

if let userDate=self.selectedDate {
    if(NSDate().timeIntervalSinceDate(userDate) >= (568024668 /*18 years in seconds*/) {
        .... /*is 18*/
    }
}

You can use NSCalendar components method and extract NSCalendarUnit.CalendarUnitYear difference from two dates: 您可以使用NSCalendar组件方法并从两个日期中提取NSCalendarUnit.CalendarUnitYear差异:

extension NSDate {
    var is18yearsOld:Bool {
        return NSDate().yearsFrom(self) > 18
    }
    func yearsFrom(date:NSDate)   -> Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: date, toDate: self, options: nil).year }
}

let dob1 = NSCalendar.currentCalendar().dateWithEra(1, year: 1970, month: 3, day: 27, hour: 7, minute: 19, second: 26, nanosecond: 0)!
let dob2 = NSCalendar.currentCalendar().dateWithEra(1, year: 2000, month: 3, day: 27, hour: 7, minute: 19, second: 26, nanosecond: 0)!
let is18years1 = dob1.is18yearsOld    // true
let is18years2 = dob2.is18yearsOld    // false

Firstly, you have to get parts of date as Int type using Calendar component methods:首先,您必须使用 Calendar 组件方法以 Int 类型获取部分日期:

// Current date
let currentDate = Date()
let calendar = Calendar.current
                
let currentYear = calendar.component(.year, from: currentDate) //year: Int
let currentMonth = calendar.component(.month, from: currentDate) //month: Int
let currentDay = calendar.component(.day, from: currentDate) //day: Int


// DatePicker's date
let yearFromDatePicker = calendar.component(.year, from: datePicker.date) //year: Int
let monthFromDatePicker = calendar.component(.month, from: datePicker.date) //month: Int
let dayFromDatePicker = calendar.component(.day, from: datePicker.date) //day: Int

and then you can compare parts of the date with each other based on your conditions.然后您可以根据您的条件将日期的各个部分相互比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM