我正在尝试使用此功能从Swift中的birthdayDate计算年龄:
var calendar : NSCalendar = NSCalendar.currentCalendar()
var dateComponentNow : NSDateComponents = calendar.components(
NSCalendarUnit.CalendarUnitYear,
fromDate: birthday,
toDate: age,
options: 0)
但我收到错误Extra argument toDate in call
在目标c中,这是代码,但我不知道为什么会出现此错误:
NSDate* birthday = ...;
NSDate* now = [NSDate date];
NSDateComponents* ageComponents = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit
fromDate:birthday
toDate:now
options:0];
NSInteger age = [ageComponents year];
是否有比这更好的形式?
答案 0 :(得分:61)
您收到错误消息,因为0
不是NSCalendarOptions
的有效值。
对于"没有选项",请使用NSCalendarOptions(0)
或简单nil
:
let ageComponents = calendar.components(.CalendarUnitYear,
fromDate: birthday,
toDate: now,
options: nil)
let age = ageComponents.year
(可以指定nil
,因为NSCalendarOptions
符合RawOptionSetType
协议,后者继承
来自NilLiteralConvertible
。)
Swift 2的更新:
let ageComponents = calendar.components(.Year,
fromDate: birthday,
toDate: now,
options: [])
Swift 3的更新:
假设使用了Swift 3类型Date
和Calendar
:
let now = Date()
let birthday: Date = ...
let calendar = Calendar.current
let ageComponents = calendar.dateComponents([.year], from: birthday, to: now)
let age = ageComponents.year!
答案 1 :(得分:16)
我创建这个方法非常简单,只需将生日日期放在方法中,这将返回Age作为Int
Readline.readline
func calcAge(birthday: String) -> Int {
let dateFormater = DateFormatter()
dateFormater.dateFormat = "MM/dd/yyyy"
let birthdayDate = dateFormater.date(from: birthday)
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: .gregorian)
let now = Date()
let calcAge = calendar.components(.year, from: birthdayDate!, to: now, options: [])
let age = calcAge.year
return age!
}
func calcAge(birthday: String) -> Int{
let dateFormater = NSDateFormatter()
dateFormater.dateFormat = "MM/dd/yyyy"
let birthdayDate = dateFormater.dateFromString(birthday)
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let calcAge = calendar.components(.Year, fromDate: birthdayDate!, toDate: now, options: [])
let age = calcAge.year
return age
}
答案 2 :(得分:3)
快速4效果很好
func getAgeFromDOF(date: String) -> (Int,Int,Int) {
let dateFormater = DateFormatter()
dateFormater.dateFormat = "YYYY-MM-dd"
let dateOfBirth = dateFormater.date(from: date)
let calender = Calendar.current
let dateComponent = calender.dateComponents([.year, .month, .day], from:
dateOfBirth!, to: Date())
return (dateComponent.year!, dateComponent.month!, dateComponent.day!)
}
let age = getAgeFromDOF(date: "2000-12-01")
print("\(age.0) Year, \(age.1) Month, \(age.2) Day")
答案 3 :(得分:2)
这适用于Swift 3
let myDOB = Calendar.current.date(from: DateComponents(year: 1994, month: 9, day: 10))!
let myAge = Calendar.current.dateComponents([.month], from: myDOB, to: Date()).month!
let years = myAge / 12
let months = myAge % 12
print("Age : \(years).\(months)")
答案 4 :(得分:1)
这对我来说是快速的工作..
{{1}}
感谢@Martin R
答案 5 :(得分:0)
这是Swift 5的最佳方法
lazy var dateFormatter : DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()
let birthday = dateFormatter.date(from: "1980-04-25")
let timeInterval = birthday?.timeIntervalSinceNow
let age = abs(Int(timeInterval! / 31556926.0))
答案 6 :(得分:0)
//创建字符串扩展名使其更容易
extension String {
func getDate(format: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
return dateFormatter.date(from: self) ?? Date()
}
}
let today = Date()
let birthDate = "1990-01-01".getDate(format: "yyyy-MM-dd")
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: birthDate, to: today)
let ageYears = components.year
//获得几岁了
let ageMonths = components.month
//其他月份
let ageDays = components.day
//额外的天数
答案 7 :(得分:-2)
这种方法很容易学习,可以快速计算年龄
class AdModel(models.Model):
id = models.IntegerField()
name = models.CharField(max_length=50)
address = models.CharField(max_length=50)
web = models.CharField(max_length=50)
struct DOB{
var date:Int?
var month:Int?
var year:Int?
}
class age{
//data Member
var dob = DOB()
var age:Int?
//member Function
func ageCalculation(){
let dtfFormet = DateFormatter()
dtfFormet.dateFormat = "MM"
let currentmonth = Int(dtfFormet.string(from: Date()))
var yearCount = 0
if(currentmonth!<dob.month!){
yearCount = -1
}
else if(currentmonth == dob.month!){
dtfFormet.dateFormat = "dd"
let currentDate = Int(dtfFormet.string(from: Date()))
if(currentDate! < dob.date!)
{
yearCount = -1
}
}
dtfFormet.dateFormat = "yyyy"
let currentdate = Int(dtfFormet.string(from: Date()))
let agecalculate = currentdate! - self.dob.year! + (yearCount)
self.age = agecalculate
}
}
ans :- 我的年龄是 :- 21