如何在目标 C 中未设置年份的情况下检查日期是否在范围内

发布于 2024-12-20 23:20:51 字数 127 浏览 3 评论 0原文

有没有办法检查 NSDate 对象是否在月日范围内,而没有在范围内设置年份。例如,我的 NSDate 等于 1956 年 2 月 2 日,我想查明该日期是否在任何年份的 1 月 10 日到 2 月 10 日之间。

先感谢您!

Is there a way to check if NSDate object is in the range of month day with out having a year set in range. For example I have NSDate eqaual to February 2 1956 and I want find out if that date is in range between January 10 and February 10 within any year.

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

感性 2024-12-27 23:20:51

使用 NSCalendar 和 NSDateComponents。

例如

// Assuming 'data' is a valid NSDate object
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit+NSDayCalendarUnit fromDate:date];
NSInteger day = components.day;
NSInteger month = components.month;
if ( (month>=startMonth && month<=endMonth) && (day>=startMonthDay && day<=endMonthDay)) {
// do your stuff
}

Use NSCalendar and NSDateComponents.

e.g.

// Assuming 'data' is a valid NSDate object
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit+NSDayCalendarUnit fromDate:date];
NSInteger day = components.day;
NSInteger month = components.month;
if ( (month>=startMonth && month<=endMonth) && (day>=startMonthDay && day<=endMonthDay)) {
// do your stuff
}
夜雨飘雪 2024-12-27 23:20:51

您应该使用 NSDate 的 -compare 消息,或者使用 -timeIntervalSince1970

NSDate *startRange;
NSDate *endRange;

// method one
if (([myDate compare:startRange] > 0) &&  ([myDate compare:endRange] < 0))
{
    // execute code here
}

// method two
if (myDate.timeIntervalSince1970 > startRange.timeIntervalSince1970 && 
    myDate.timeIntervalSince1970 < endRange.timeIntervalSince1970)
{
     // execute code here
}

You should use NSDate's -compare message, or, use -timeIntervalSince1970

NSDate *startRange;
NSDate *endRange;

// method one
if (([myDate compare:startRange] > 0) &&  ([myDate compare:endRange] < 0))
{
    // execute code here
}

// method two
if (myDate.timeIntervalSince1970 > startRange.timeIntervalSince1970 && 
    myDate.timeIntervalSince1970 < endRange.timeIntervalSince1970)
{
     // execute code here
}
就像说晚安 2024-12-27 23:20:51

您可以获取当前的 NSCalendar 实例并使用 Components:forDate: 方法,然后从结果 NSDateComponents 对象。

You can get the current NSCalendar instance and use the components:forDate: method, then extract the month and date components from the resulting NSDateComponents object.

农村范ル 2024-12-27 23:20:51

您可以为此使用 NSDateComponents

    NSDate *date = [NSDate date];
    // setting units we would like to use in future
    unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSWeekdayCalendarUnit;
    // creating NSCalendar object
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    // extracting components from date
    NSDateComponents *components = [calendar components:units fromDate:date];

    if (components.month <= 2 && components.month >= 1) {
         if (components.month == 2) {
                if (components.day <= 10) {
                       // Date is in range
                }
         } else if (components.month == 2) {
                if (components.day >= 10) {
                       // Date is in range
         }
    }

You can use NSDateComponents for this

    NSDate *date = [NSDate date];
    // setting units we would like to use in future
    unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSWeekdayCalendarUnit;
    // creating NSCalendar object
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    // extracting components from date
    NSDateComponents *components = [calendar components:units fromDate:date];

    if (components.month <= 2 && components.month >= 1) {
         if (components.month == 2) {
                if (components.day <= 10) {
                       // Date is in range
                }
         } else if (components.month == 2) {
                if (components.day >= 10) {
                       // Date is in range
         }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文