一个日历年的两个NSDates之间的天数迅速 [英] Days between 2 NSDates in a calendar year swift

查看:123
本文介绍了一个日历年的两个NSDates之间的天数迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个生日提醒应用程序。我希望用户能够看到有多少天直到有人的下一个生日。



假设我有一个 NSDate() = 2015-06-30 07: 21:47 +0000



而生日的 NSDate = 1985-08-29 12:00:00 +0000



如何获得下一个生日的天数?我已经使用像这个这样的日子,这是自从实际的日期起的天数开始(有数千个出生日期)。然后,我会添加365的数值差异,直到它是积极的,但仍然返回一个数字。我假设是由于闰年或某事。



有没有办法可以实现?不知不觉间平均年份,以便从下一个生日而不是原始的出生日期进行比较?



编辑:



这是我使用的功能:

  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)

返回组件。 day
}

随着我发布的示例日期,我会使用它: p>

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

无论是正面还是负面,数字都在上千。我正在等待下一个日历生日的天数。

解决方案

你需要的是计算<今天之后的(日/月
组成部分)的下一次

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

备注:




  • MatchNextTimePreservingSmallerUnits 选项的目的是
    ,如果生日是在二月二十九日(在公历中),其下一次发生将是
    如果年份不是闰年,则计算为3月1日。


  • 您可能需要先查看生日是否今天,因为它似乎是
    nextDateAfterDate()将在这种情况下返回下一个生日。




然后,您可以像往常一样计算天数差异:

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





Swift 2.2更新(Xcode 7.3 ):

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

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






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,matched:dayAndMonth,
matchedPolicy:.nextTimePreservingSmallerComponents)!

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


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.

解决方案

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!)

这篇关于一个日历年的两个NSDates之间的天数迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆