未来 10 天是我的联系人的生日吗

发布于 2024-11-03 16:30:34 字数 135 浏览 0 评论 0原文

我正在尝试计算地址簿中的任何联系人是否会在接下来的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北渚 2024-11-10 16:30:35

将生日更改为今年(如果生日已经在今年,则更改为明年)并使用 NSDateComponents 和 NSCalendar 进行计算。

看起来有点复杂,但你可以这样做:

NSDate *birthDay = ... // [calendar dateFromComponents:myBirthDay];

NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDateComponents *thisYearComponents = [calendar components:NSYearCalendarUnit fromDate:[NSDate date]];
NSDateComponents *birthDayComponents = [calendar components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:birthDay];
[birthDayComponents setYear:[thisYearComponents year]];

NSDate *birthDayThisYear = [calendar dateFromComponents:birthDayComponents];

NSDateComponents *difference = [calendar components:NSDayCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0];
if ([difference day] < 0) {
    // this years birthday is already over. calculate distance to next years birthday
    [birthDayComponents setYear:[thisYearComponents year]+1];
    birthDayThisYear = [calendar dateFromComponents:birthDayComponents];
    difference = [calendar components:NSDayCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0];
}


NSLog(@"%i days until birthday", [difference day]);

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:

NSDate *birthDay = ... // [calendar dateFromComponents:myBirthDay];

NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDateComponents *thisYearComponents = [calendar components:NSYearCalendarUnit fromDate:[NSDate date]];
NSDateComponents *birthDayComponents = [calendar components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:birthDay];
[birthDayComponents setYear:[thisYearComponents year]];

NSDate *birthDayThisYear = [calendar dateFromComponents:birthDayComponents];

NSDateComponents *difference = [calendar components:NSDayCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0];
if ([difference day] < 0) {
    // this years birthday is already over. calculate distance to next years birthday
    [birthDayComponents setYear:[thisYearComponents year]+1];
    birthDayThisYear = [calendar dateFromComponents:birthDayComponents];
    difference = [calendar components:NSDayCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0];
}


NSLog(@"%i days until birthday", [difference day]);
梦里的微风 2024-11-10 16:30:35

检索联系人的出生日期后:

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

痕至 2024-11-10 16:30:35

与您的联系人 NSDate 一起使用以下 NSDate 函数。

- (NSTimeInterval)timeIntervalSinceNow

上述函数返回接收器与当前日期和时间之间的间隔。

   NSTimeInterval interval = [date1 timeIntervalSinceNow:myContactDate];

NSTimeInterval 是以秒为单位的经过时间(表示为浮点数)。然后,您可以除以 86400(一天中的秒数)并四舍五入到最接近的整数。

NSInteger days = interval / 86400;

现在您有天数...

编辑:

您还可以使用NSCalendarNSDateComponents仅获取两个日期之间的日期部分。

- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts

使用下面的代码作为参考。 (摘自 Apple 文档NS日历)

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *startDate = ...;
    NSDate *endDate = ...;
    unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

    NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];
    int months = [comps month];
    int days = [comps day];

Use the below NSDate function with your contact NSDate.

- (NSTimeInterval)timeIntervalSinceNow

The above function returns the interval between the receiver and the current date and time.

   NSTimeInterval interval = [date1 timeIntervalSinceNow:myContactDate];

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.

NSInteger days = interval / 86400;

Now you have number of days...

EDITED:

You could also use the NSCalendar and NSDateComponents to only get the day component between two date.

- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts

Use the below code as reference. (Taken From Apple Documentation for NSCalendar)

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *startDate = ...;
    NSDate *endDate = ...;
    unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

    NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];
    int months = [comps month];
    int days = [comps day];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文