如何根据 TAPKU 日历中的日期检查 NSDate

发布于 2024-12-12 03:13:16 字数 148 浏览 0 评论 0原文

我正在使用核心数据来存储我的项目的对象。对象中有一个日期字段。我想检查 iPhone 的 Tapku 日历组件的单击日期。我的对象上的日期格式为

2011-01-10 并且是本地化的 date 。

我如何检查从 tapku 日历控件返回的日期的日期。

I am using core data to store objects for my project.There is a date field in the object .I want to check against the clicked date of the Tapku calendar component for iphone. The date on my object is in the format

2011-01-10 and it is a localized date .

How can i check the date against the date returned from the tapku calendar control.

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

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

发布评论

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

评论(2

太阳公公是暖光 2024-12-19 03:13:16

是的,你可以,这是我为使其工作所做的:

在方法上: (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthViewmarksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate

  • 进行正常的核心数据获取获取对象数组
  • 开始使用 [NSArrayindexsOfObjectsPassingTest:] 方法每天检查从 startDate 到 lastDate
  • 对于所有通过测试的添加和对象添加到标记数组中,并添加一个字典,其中测试的日期作为键,获取的对象作为值

警告:确保将 NSDate 对象转换为无时间和 GMT:0 日期,以便进行日期比较(已学习)这是困难的方法...)

这是一些代码...

self.marksArray = [NSMutableArray array];
self.dataDictionary = [NSMutableDictionary dictionary];

NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) fromDate:start];

NSDate *ds = [cal dateFromComponents:comp];

// Init offset components to increment days in the loop by one each time
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];


while (YES) {
    // Is the date beyond the last date? If so, exit the loop.
    // NSOrderedDescending = the left value is greater than the right
    if ([ds compare:end] == NSOrderedDescending) {
        break;
    }

    NSIndexSet *indexes = [arrayWithObjectsFromCoreData indexesOfObjectsPassingTest:
                           ^BOOL (id el, NSUInteger i, BOOL *stop) {
                               NSDate *upd = [(YOUR_ENTINY *)el updated];

                               NSDate *day=[self midnightUTC:upd]; 
                               if([ds isSameDay:day]){ 
                                   return TRUE;
                               }
                               return FALSE;
                           }];



    if (indexes.count>0) {

        [self.marksArray addObject:[NSNumber numberWithBool:YES]];
        [self.dataDictionary setObject:[wods objectsAtIndexes:indexes] forKey:ds];


    }else {
        //no wods in this date
        [self.dataArray addObject:[NSNumber numberWithBool:NO]];
    } 

    // Increment day using offset components (ie, 1 day in this instance)
    ds = [cal dateByAddingComponents:offsetComponents toDate:ds options:0];



}
[offsetComponents release];

午夜UTC方法可以在这里找到:
http://www.voidasterisk.org /post/4209842795/how-to-calculate-nsdate-of-midnight-in-ios

Yes you can, here is what I did to make it work:

on the method: (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate

  • Make the normal core data fetch to get an array of objects
  • Start checking every day from startDate to lastDate with the [NSArray indexesOfObjectsPassingTest:] method
  • For all that passes the test add and object to the marks array and add a dictionary with the tested day as key and the fetched objects as values

WARNING: make sure you cast your NSDate objects to a no time and GMT:0 dates in order to make the day comparison to work (learned this the hard way...)

Here is some code...

self.marksArray = [NSMutableArray array];
self.dataDictionary = [NSMutableDictionary dictionary];

NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) fromDate:start];

NSDate *ds = [cal dateFromComponents:comp];

// Init offset components to increment days in the loop by one each time
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];


while (YES) {
    // Is the date beyond the last date? If so, exit the loop.
    // NSOrderedDescending = the left value is greater than the right
    if ([ds compare:end] == NSOrderedDescending) {
        break;
    }

    NSIndexSet *indexes = [arrayWithObjectsFromCoreData indexesOfObjectsPassingTest:
                           ^BOOL (id el, NSUInteger i, BOOL *stop) {
                               NSDate *upd = [(YOUR_ENTINY *)el updated];

                               NSDate *day=[self midnightUTC:upd]; 
                               if([ds isSameDay:day]){ 
                                   return TRUE;
                               }
                               return FALSE;
                           }];



    if (indexes.count>0) {

        [self.marksArray addObject:[NSNumber numberWithBool:YES]];
        [self.dataDictionary setObject:[wods objectsAtIndexes:indexes] forKey:ds];


    }else {
        //no wods in this date
        [self.dataArray addObject:[NSNumber numberWithBool:NO]];
    } 

    // Increment day using offset components (ie, 1 day in this instance)
    ds = [cal dateByAddingComponents:offsetComponents toDate:ds options:0];



}
[offsetComponents release];

The midnightUTC method can be found here:
http://www.voidasterisk.org/post/4209842795/how-to-calculate-nsdate-of-midnight-in-ios

隱形的亼 2024-12-19 03:13:16

可以提供更多信息,但 tapku 库在 NSDate 上有一个名为 isSameDay 的扩展方法,因此您可以使用它:

if ([myDate isSameDay:returnedDate])
    // Do something here

这就是您正在寻找的吗?

Could do with a bit more information but the tapku library has an extension method on NSDate called isSameDay so you could use that:

if ([myDate isSameDay:returnedDate])
    // Do something here

Is that what you are looking for?

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