如何按数据中的某些特定顺序对日期进行排序 [iphone]

发布于 2024-12-06 15:45:31 字数 338 浏览 0 评论 0原文

我从数据库中获得了一些事件的日期及其升序。

现在,我想要一些按钮..

在此结构中...

--######## 上周######----
--######## 昨天 #####----

--######## 今天 #####----

--########明天#####----
--######## 下周######----

现在当我按下受人尊敬的按钮时.. 应该显示该受尊重的持续时间内的事件..

我的意思是所有数据都应该与今天进行比较,并且所有数据都应该按这个顺序缩短..

任何人都可以给我一个想法或逻辑吗?

I have got some dates for some events and their ascending order from the database .

Now , I want want some buttons ..

in this structure ...

--######## Last Week #####----
--######## yesterday #####----

--######## today #####----

--######## tomorrow #####----
--######## Next Week #####----

Now when I press the respected buttons ..
the events in that respected duration should be shown up ..

I mean all the data should be compared with today and all the data should be shorted by this order..

can anyone give me a idea or logic for this ?

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

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

发布评论

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

评论(1

痴意少年 2024-12-13 15:45:32

处理日期的最佳方法是使用 NSDateComponents。
例如,要获取明天的日期,请使用:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comp = [[[NSDateComponents alloc] init] autorelease];
[comp setDay:1];
NSDate *today = [NSDate date];
NSDate *tomorrow = [cal dateByAddingComponents:comp toDate:today options:0];

因此,您需要做的是:

  1. 获取所有相关日期类别的日期
  2. 当您按下按钮时,构建一个谓词以仅检索两个日期之间的对象。例如,获取“明天”类别中的所有对象:

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"date > %@ AND date < %@", 明天,下周];
    

您可以使用一个方便的小方法来获取日期,通过使用此方法向 NSDate 添加类别:

@implementation NSDate (Components)

-(NSDate *) dateByAddingCalendarUnit:(NSCalendarUnit)calendarUnit andCount:(NSInteger) count
{
    NSDateComponents *comp = [[NSDateComponents alloc] init];
    switch (calendarUnit) {
        case NSSecondCalendarUnit:
            [comp setSecond:count];
            break; 
        case NSMinuteCalendarUnit:
            [comp setMinute:count];
            break; 
        case NSHourCalendarUnit:
            [comp setHour:count];
            break; 
        case NSDayCalendarUnit:
            [comp setDay:count];
            break;  
        case NSWeekCalendarUnit:
            [comp setWeek:count];
            break; 
        case NSWeekdayCalendarUnit:
            [comp setWeekday:count];
            break;
        case NSWeekdayOrdinalCalendarUnit:
            [comp setWeekdayOrdinal:count];
            break; 
        case NSMonthCalendarUnit:
            [comp setMonth:count];
            break; 
        case NSYearCalendarUnit:
            [comp setYear:count];
            break; 
        case NSEraCalendarUnit:
            [comp setEra:count];
            break; 
        default:
            break;
    }

    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *newDate = [cal dateByAddingComponents:comp toDate:self options:0];
    [comp release];
    return newDate;
}

@end

并像这样使用此方法:

NSDate *lastWeek = [[NSDate date] dateByAddingCalendarUnit:NSWeekCalendarUnit andCount:-1];

The best way to deal with dates is by using NSDateComponents.
for example, to get the date for tomorrow use:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comp = [[[NSDateComponents alloc] init] autorelease];
[comp setDay:1];
NSDate *today = [NSDate date];
NSDate *tomorrow = [cal dateByAddingComponents:comp toDate:today options:0];

So, what you need to do is:

  1. Get the dates for all the relevant date categories
  2. When you push a button, build a predicate to retrieve only the objects between the two dates. For example, getting all the objects in the "tomorrow" category:

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"date > %@ AND date < %@", tomorrow, nextWeek];
    

You can use a small convenient method for getting the dates, by adding a category to NSDate with this method:

@implementation NSDate (Components)

-(NSDate *) dateByAddingCalendarUnit:(NSCalendarUnit)calendarUnit andCount:(NSInteger) count
{
    NSDateComponents *comp = [[NSDateComponents alloc] init];
    switch (calendarUnit) {
        case NSSecondCalendarUnit:
            [comp setSecond:count];
            break; 
        case NSMinuteCalendarUnit:
            [comp setMinute:count];
            break; 
        case NSHourCalendarUnit:
            [comp setHour:count];
            break; 
        case NSDayCalendarUnit:
            [comp setDay:count];
            break;  
        case NSWeekCalendarUnit:
            [comp setWeek:count];
            break; 
        case NSWeekdayCalendarUnit:
            [comp setWeekday:count];
            break;
        case NSWeekdayOrdinalCalendarUnit:
            [comp setWeekdayOrdinal:count];
            break; 
        case NSMonthCalendarUnit:
            [comp setMonth:count];
            break; 
        case NSYearCalendarUnit:
            [comp setYear:count];
            break; 
        case NSEraCalendarUnit:
            [comp setEra:count];
            break; 
        default:
            break;
    }

    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *newDate = [cal dateByAddingComponents:comp toDate:self options:0];
    [comp release];
    return newDate;
}

@end

And use this method like this:

NSDate *lastWeek = [[NSDate date] dateByAddingCalendarUnit:NSWeekCalendarUnit andCount:-1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文