如果该日期已经过去,则将日期年份增加1

时间:2013-03-13 11:42:32

标签: iphone ios date

我有一个包含如下所示出生日期的数组:

 Array(
"11/07/2013",
"07/10/2013",
"20/02/2013"
)

现在我想根据日期是否已经过来创建一个新数组。在2013年写下这个问题,如果当前日期已经过去,那么我们将把该日期的年份更改为2014年。如果尚未通过,那么我们将保留2013年的日期。

例如:

NewArray(
 "11/07/2013",    no change cus this date hasnt passed yet
 "07/10/2013",     no change same as above
 "20/02/2014"     **as date has already passed thats why 2014**

我正在使用以下代码

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSString *curYear = [dateFormatter stringFromDate:[NSDate date]];
NSString *nextYear = [NSString stringWithFormat: @"%d", ([curYear intValue] + 1)];
for(int i = 0; i < [_newlymadeArray count]; i++)
{
    NSString *dateStr = [_newlymadeArray objectAtIndex:i];
    NSComparisonResult comResult = [[dateFormatter dateFromString:dateStr] compare:    [NSDate date]];
    if(comResult == NSOrderedAscending)
    {
        [dateStr stringByReplacingOccurrencesOfString:curYear withString:nextYear];
        [_newlymadeArray replaceObjectAtIndex:i withObject:dateStr];
         NSLog(@"_newlymadeArray%@",_newlymadeArray);
    }
NSLog(@"_newlymadeArray%@",_newlymadeArray);

然而,这是我在NSLog _newlymadeArray

时得到的
 after replacing (
 "11/07/2013",
 "07/10/2013",
 "20/02/2013"       
 )

索引2 应该是“20/02/2014”而不是2013年的日期。什么可能导致我的问题,我该如何解决?

4 个答案:

答案 0 :(得分:2)

我对您的代码进行了一些修改,它可以按照您的意愿运行。

在我的代码中,我比较了日期,即当前日期的升序形式。如果它满足条件,那么我从匹配日期获取YEAR,由DateFormatter“yyyy”获取。然后我简单地将今年增加1,并在旧日期取代今年,即“20/02 / 2013 ”到“20/02 / 2014

array = [[NSMutableArray alloc] initWithObjects:@"11/07/2013",@"07/10/2013",@"20/02/2013", nil];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
for(int i = 0; i < [array count]; i++)
{
    NSString *dateStr = [array objectAtIndex:i];
    NSComparisonResult comResult = [[dateFormatter dateFromString:dateStr] compare:    [NSDate date]];
    if(comResult == NSOrderedAscending)
    {
        NSDateFormatter *yrFormatter = [[NSDateFormatter alloc] init];
        [yrFormatter setDateFormat:@"yyyy"];
        NSString *curYear = [yrFormatter stringFromDate:[NSDate date]];
        NSString *nextYear = [NSString stringWithFormat: @"%d", ([curYear intValue] + 1)];

        NSLog(@"%@",curYear);
        NSLog(@"%@",nextYear);

        dateStr = [dateStr stringByReplacingOccurrencesOfString:curYear withString:nextYear];
        NSLog(@"%@",dateStr);
        [array replaceObjectAtIndex:i withObject:dateStr];
        NSLog(@"_newlymadeArray%@",array);
    }
    NSLog(@"_newlymadeArray%@",array);
}

这似乎完美无缺,所以我希望它可以帮到你。

答案 1 :(得分:1)

请亲爱的尝试使用这个。我想这个可以帮助

 NSCalendar*       calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents* components = [[NSDateComponents alloc] init];
    components.year = 1;
    NSDate* newDate = [calendar dateByAddingComponents: components toDate:@"YourDate" options: 0];

否则你可以使用这一个代码。

   if (comResult == NSOrderedSame)
   {
       NSCalendar*       calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents* components = [[NSDateComponents alloc] init];
    components.year = 1;

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd/MM/yyyy"];

    NSDate *date = [formatter dateFromString:@"11/07/2013"];

    NSDate* newDate = [calendar dateByAddingComponents: components toDate:date options: 0];
   // here replace your array object with this "newDate"

   }

答案 2 :(得分:0)

使用NSDate compare函数将您的数组日期与今天的日期进行比较。以下是详细信息:

NSString *arrayDateString = @"20/02/2013" // fetch this string from your array
NSDate *todaysDate = [NSDate date];

NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy"];
NSDate* d = [df dateFromString:arrayDateString];

// now compare this date (d) with todaysDate using NSDate function
if ([d compare:todaysdate]== NSOrderedAscending)  
{//write your code here}

如果结果为NSOrderedAscending,则表示数组日期早于今天的日期。

因此,对于该日期,更新年份使用NSDateComponents增加1:

NSDateComponents *dayComponent = [[[NSDateComponents alloc] init] autorelease];
dayComponent.year = 1;

NSCalendar *theCalendar = [NSCalendar currentCalendar];
dateToBeIncremented = [theCalendar dateByAddingComponents:dayComponent toDate:dateToBeIncremented options:0];

或者您可以使用NSDate功能本身:

NSDate *now = arrayDate;
int yearsToAdd = 1;
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*365*year];

但第二种选择并非完全证明 - 因为闰年问题。

希望这两个选项可以帮助您解决问题。

答案 3 :(得分:0)

NSArray * array = [[NSArray alloc]   initWithObjects:@"11/07/2013",@"07/10/2013",@"20/02/2013", nil];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSMutableArray *output = [NSMutableArray new];

for(int i = 0; i < [array count]; i++)
{
    NSString *dateStr = [array objectAtIndex:i];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    if ([date compare:now] == NSOrderedAscending)
    {

        [components setYear:1];
        date = [calendar dateByAddingComponents:components toDate:date options:0];

    }
    dateStr = [dateFormatter stringFromDate:date];
    [output addObject:dateStr];

}
NSLog(@"Result : %@",output);