未来 10 天是我的联系人的生日吗
我正在尝试计算地址簿中的任何联系人是否会在接下来的 10 天内过生日。网上有很多代码可以比较日期,但我只想比较日期和月份。例如,如果联系人出生于 05/01/1960(假设今天是 04/24/2011),那么我只想计算距离他们的生日只有六天。帮助表示赞赏。
I am trying to calculate whether any of my Address Book's contacts have a birthday in the next 10 days. There's plenty of code on line to compare dates, but I only want to compare day and month. For example, if a contact was born 05/01/1960 (and assuming today is 04/24/2011), then I only want to calculate that there are only six days till their birthday. Help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将生日更改为今年(如果生日已经在今年,则更改为明年)并使用 NSDateComponents 和 NSCalendar 进行计算。
看起来有点复杂,但你可以这样做:
Change the birthday to this year (or next year if the birthday was already in this year) and calculate with NSDateComponents and NSCalendar.
Looks a bit complicated, but you could do it like this:
检索联系人的出生日期后:
ABAddressBook 获取生日属性,
您需要调整年份部分:
NSDate - 更改年份值
然后,您可以计算距离她生日还有多少天:
NSDate 倒计时(以天为单位)
Once you have retrieved your contact's birthdate:
ABAddressBook get birthday property
you need to adjust the year component:
NSDate - Changing the year value
Then, you can compute the number of days till her birthday:
NSDate countdown in days
与您的联系人
NSDate
一起使用以下 NSDate 函数。上述函数返回接收器与当前日期和时间之间的间隔。
NSTimeInterval
是以秒为单位的经过时间(表示为浮点数)。然后,您可以除以 86400(一天中的秒数)并四舍五入到最接近的整数。现在您有天数...
编辑:
您还可以使用
NSCalendar
和NSDateComponents
仅获取两个日期之间的日期部分。使用下面的代码作为参考。 (摘自 Apple 文档NS日历)
Use the below NSDate function with your contact
NSDate
.The above function returns the interval between the receiver and the current date and time.
NSTimeInterval
is the elapsed time in seconds (expressed as a floating point number). You could then divide by 86400, which is the number of seconds in a day and round to the nearest integer.Now you have number of days...
EDITED:
You could also use the
NSCalendar
andNSDateComponents
to only get the day component between two date.Use the below code as reference. (Taken From Apple Documentation for NSCalendar)