将日期字符串与NSPredicate进行比较 [英] Comparing date string with NSPredicate

查看:57
本文介绍了将日期字符串与NSPredicate进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用按日期排序的 NSPredicate 过滤集合,其中日期是 MMMM dd 中的 NSString (例如3月24日)(不是NSDate.).

I was wondering if there's anyway to filter a set using NSPredicate sorted by date, where the date is an NSString in MMMM dd (i.e. March 24) format (it's NOT an NSDate.).

推荐答案

如果将日期作为字符串存储在CoreData中,则谓词很简单.

If you are storing the date as a string in CoreData, the predicate is simple.

    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"theDate like %@", theDateString];

但是我猜测日期存储为NSDate对象.查看使用 NSDateFormatter 将您的字符串转换为NSDate.

But I'm guessing that the date is stored as an NSDate object. Look into using NSDateFormatter to convert your string to an NSDate.

NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMMM dd"];
NSDate* theDate = [df dateFromString:theDateString];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SomeDate >= %@", theDate];

这篇关于将日期字符串与NSPredicate进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆