[英]comparing the date
i am trying to compare the date but its not comparing the date like 31-dec-2011 with 1-01-2012. 我正在尝试比较日期,但未将日期与2012年1月31日进行比较(例如,2011年12月31日)。 i was trying to remove values at 31-dec-2011 and any other values previous then 1-01-2012.
我试图删除2011年12月31日的值以及2012年1月1日之前的任何其他值。 here i am doing this: i want to show the values at today's date and on upcoming dates.
在这里,我正在这样做:我想显示今天的日期和即将到来的日期的值。
-(void)showcurrentdate
{
NSDate *objdate=[NSDate date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"dd-MM-YYYY"];
NSDate *str1= (NSDate *)[dateformatter stringFromDate:objdate];
NSLog(@"configuregameArray......%d",[configuregameArray count]);
if([configuregameArray count]>=1)
{
NSMutableArray* indexarray= [[NSMutableArray alloc]init];
for(int k =0; k<[configuregameArray count]; k++)
{
NSDate *datefromarray = (NSDate *)[[configuregameArray objectAtIndex:k] objectForKey:@"gd"];
NSComparisonResult result = [datefromarray compare:str1];
switch (result)
{
case NSOrderedAscending:
NSLog(@"%@ is greater from %@", str1 ,datefromarray);
indexNumber = [NSString stringWithFormat:@"%d",k];
[indexarray addObject:indexNumber];
NSLog(@"indexarray......%@",indexarray);
break;
default:
NSLog(@"going fine");
break;
}
}
NSMutableIndexSet *discardedItems = [NSMutableIndexSet indexSet];
for(int i=0; i<[indexarray count]; i++)
{
NSInteger removeindex = [[indexarray objectAtIndex:i]intValue];
[discardedItems addIndex:removeindex];
}
[configuregameArray removeObjectsAtIndexes:discardedItems];
}
NSLog(@"configuregameArray......%@",configuregameArray);
}
Given that you have your dates stored as strings in another array and that you want to end up with an array of strings, filtering out only the dates that occur after a certain date would take these steps: 假设您将日期存储为字符串并存储在另一个数组中,并且您希望最终得到一个字符串数组,那么仅过滤出在某个日期之后发生的日期将采取以下步骤:
Obviously it would be better to store dates in the array. 显然,将日期存储在数组中会更好。 This make all date calculations much easier.
这使所有日期的计算变得更加容易。 Then you could filter the array with a one line NSPredicate instead of many lines of code.
然后,您可以使用一行NSPredicate而不是多行代码来过滤数组。 It is generally a bad design to store objects as strings.
将对象存储为字符串通常是一个糟糕的设计。 A much better practice is to store the objects as objects and convert them to strings when presenting them in a user interface.
更好的做法是将对象存储为对象,并在用户界面中显示它们时将它们转换为字符串。 This was you can change how the objects are formatted without having to change how they are stored, converted, and used.
这是您可以更改对象格式化的方式,而不必更改它们的存储,转换和使用方式。 It is a separation of concerns where the interface who is concerned about showing dates as strings converts dates to strings while the model who is concerned about dates uses dates and doesn't care about strings at all.
当关注日期显示为字符串的接口将日期转换为字符串,而关注日期的模型使用日期而不关心字符串时,这是关注点分离。
Anyhow, code for the above steps to filter the date strings in your question would look something like below. 无论如何,上述步骤的代码可以过滤问题中的日期字符串 ,如下所示。 If you want to filter using todays date you can use
[NSDate date];
如果要使用今天的日期进行过滤,则可以使用
[NSDate date];
to get todays date as a date and not a string. 获取今天的日期作为日期而不是字符串。
// Create some sample date strings
NSArray *dateStrings = [NSArray arrayWithObjects:@"01-01-2012", @"03-01-2012", @"01-03-2012", @"10-08-2011", @"06-02-2002", @"20-04-1999", @"01-01-2012", @"31-12-2011", @"07-03-2014", @"18-09-3456", @"27-07-1924", nil];
// The same kind date format as the question
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd-MM-YYYY";
// The date that other dates are filtered using
NSDate *filterDate = [dateFormatter dateFromString:@"01-01-2012"];
///////////// OUTPUT /////////////
NSLog(@"BEFORE: %@", dateStrings);
//////////////////////////////////
// Get the indexes of all dates after the filterDate.
NSIndexSet *newerDateIndexes = [dateStrings indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
NSString *dateString = (NSString *)obj; // The string stored in the array
NSDate *date = [dateFormatter dateFromString:dateString]; // Use the dateFormatter to get a date from the array
// Add this index to the index set if the date occurs after the filterDate.
return ([date compare:filterDate] != NSOrderedAscending);
}];
// A new array with only the dates after the filterDate
NSArray *filteredDateString = [dateStrings objectsAtIndexes:newerDateIndexes];
///////////// OUTPUT ///////////////////
NSLog(@"AFTER: %@", filteredDateString);
////////////////////////////////////////
You cannot simply cast one class to another except of they're related (casting to super/subclass). 除了它们之间的相关性之外,您不能简单地将一个类转换为另一类(广播到父类/子类)。
So you should do it like this: 因此,您应该这样做:
NSDate
instances in your array. NSDate
实例存储在阵列中。 Then all you have to do is compare the dates with your objdate
. objdate
进行比较。 That would be the best solution. [dateformatter dateFromString:]
) and compare the resulting date with your objdate
. [dateformatter dateFromString:]
),然后将结果日期与objdate
进行比较。 YYYY-MM-dd
. YYYY-MM-dd
的反向格式,则不应比较字符串表示形式,因为会得到“奇怪的”结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.