iOS:比较两个小于或等于的 NSDates [英] iOS: Compare two less or equal NSDates
问题描述
我想比较两个日期并在一个日期小于或等于另一个时执行某些操作,我尝试这样做:
I want to compare two dates and do something when one is less or equal than the other, I tried to do it this way:
if ([self.system.systemInbetriebnahme compare:date] == NSOrderedDescending)
return nil;
但是这消除了它们相等的情况有没有办法将日期与 >=
进行比较?
But this removes the case when they are both equal is there a way to compare dates with >=
?
谢谢
更多细节:我有一个取自一个二维数组的日期,我有一个存储在核心数据中的日期(通过日期选择器),我通过一个循环来比较两个日期规则是:数组中的日期必须大于或等于存储在数据库中的输入日期.
More details: I have a date taken from a double dimension array, and i have a date stored in core data(through date picker), and i go through a loop to compare the two dates with the rule that: Date in array must be greater or equal than input date stored in database.
最新
我导入了一个 csv 文件,在解析和格式化后给了我两行(一行用于数值,另一行用于日期).在这里,我对日期感兴趣.
I import a csv file which after parsing and formatting gives me two rows(one for number value and one for dates). Here I am interested in dates.
我将所有这些存储在一个数组中,并在此函数中检查数据的一致性后保存:
I store all these in an array and save after checking consistency of the data in this function:
for (NSArray * row in array) {
// Handle import if there are at least two rows
if (row.count > 1) {
dispatch_sync(dispatch_get_main_queue(), ^{
// Try to import value,date formatted row
double value = [row[0] doubleValue];
NSDate * date = [dateFormatter dateFromString:row[1]];
//else {
// Try to import date,value row
if (date && value != 0.0) {
[self addReading:value forDate:date];
}
在这里我调用函数 addreading
来检查某些规则的错误,如果一切正常,那么我保存托管对象上下文.
In here i call the function addreading
to check for errors for some rules and if everything is okey then i save the managedObject Context.
if ([installationdatum compare:date] != NSOrderedAscending)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"installation date is bigger than data"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil]
[alert show];
return nil;
} else
{ //Do nothing
}
我的一个规则是获取数组中的第一行元素,并将其日期与已存储在数据库中的输入进行比较,该输入是 self.system.systemInbetriebnahme
毕竟,我在 tableview 中列出了所有结果数组,尽管当它等于 self.system.systemInbetriebnahme
时,永远不会显示匹配 NSOrderedSame
的值
After all this I list all the resulting array in a tableview, though value matching NSOrderedSame
is never shown when it is equal to self.system.systemInbetriebnahme
推荐答案
只需检查不升序即可.
if ([self.system.systemInbetriebnahme compare:date] != NSOrderedAscending)
return nil;
这篇关于iOS:比较两个小于或等于的 NSDates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!