Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
957 views
in Technique[技术] by (71.8m points)

ios - Days between 2 NSDates in a calendar year swift

I'm building a birthday reminder app. I want the user to be able to see how many days it is until somebody's next birthday.

Let's say I have an NSDate() = 2015-06-30 07:21:47 +0000

And the NSDate for the birthday = 1985-08-29 12:00:00 +0000

How would I get the number of days until the next birthday? I've used something like this which gives a negative number of days since the date's actual beginning (which are in the thousands with birthdates). Then with that I would add 365 to the numerical difference until it was positive but it still returns a wonky number. I'm assuming due to leap years or something.

Is there a method to this I can implement? Somehow equalize the year components so that it's always comparing from the next birthday and not the original birthdate?

Edit:

Here is the function I am using:

func daysBetween(date1: NSDate, date2: NSDate) -> Int {

    var calendar: NSCalendar = NSCalendar.currentCalendar()

    let date1 = calendar.startOfDayForDate(date1)
    let date2 = calendar.startOfDayForDate(date2)

    let flags = NSCalendarUnit.CalendarUnitDay
    let components = calendar.components(flags, fromDate: date1, toDate: date2, options: nil)

    return components.day
}

With the example dates I posted I would use it like this:

// sampleDate = 1985-08-29 12:00:00 +0000
daysBetween(sampleDate, date2: NSDate())
// --> 10897

Whether positive or negative, the number is in the thousands. I'm looking to equalize that into number of days to the next calendar birthday.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you need is to compute the next occurrence of the (day and month component of the) birthday after today:

let cal = NSCalendar.currentCalendar()
let today = cal.startOfDayForDate(NSDate())
let dayAndMonth = cal.components(.CalendarUnitDay | .CalendarUnitMonth,
    fromDate: birthday)
let nextBirthDay = cal.nextDateAfterDate(today,
    matchingComponents: dayAndMonth,
    options: .MatchNextTimePreservingSmallerUnits)!

Remarks:

  • The purpose of the MatchNextTimePreservingSmallerUnits option is that if the birthday is on February 29 (in the Gregorian calendar), its next occurrence will be computed as March 1 if the year is not a leap year.

  • You might need to check first if the birthday is today, as it seems that nextDateAfterDate() would return the next birthday in that case.

Then you can compute the difference in days as usual:

let diff = cal.components(.CalendarUnitDay,
    fromDate: today,
    toDate: nextBirthDay,
    options: nil)
println(diff.day)

Update for Swift 2.2 (Xcode 7.3):

let cal = NSCalendar.currentCalendar()
let today = cal.startOfDayForDate(NSDate())
let dayAndMonth = cal.components([.Day, .Month],
                                 fromDate: birthday)
let nextBirthDay = cal.nextDateAfterDate(today,
                                         matchingComponents: dayAndMonth,
                                         options: .MatchNextTimePreservingSmallerUnits)!

let diff = cal.components(.Day,
                          fromDate: today,
                          toDate: nextBirthDay,
                          options: [])
print(diff.day)

Update for Swift 3 (Xcode 8 GM):

let cal = Calendar.current
let today = cal.startOfDay(for: Date())
let dayAndMonth = cal.dateComponents([.day, .month], from: birthday)
let nextBirthDay = cal.nextDate(after: today, matching: dayAndMonth,
                                matchingPolicy: .nextTimePreservingSmallerComponents)!

let diff = cal.dateComponents([.day], from: today, to: nextBirthDay)
print(diff.day!)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...