iphone - 如果日期的年份已经过去,则将日期的年份增加一

标签 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年日期。是什么导致我的问题,如何解决?

最佳答案

我已经对您的代码进行了一些修改,并且可以根据需要运行。

在我的代码中,我比较了日期,它是当前日期的升序形式。如果满足条件,则我已通过DateFormatter“yyyy”从匹配的日期获取了YEAR。然后我将今年简单地增加1,并用旧的Date替换今年,即“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);
}

这似乎工作得很好,所以希望对您有所帮助。

关于iphone - 如果日期的年份已经过去,则将日期的年份增加一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15384325/

相关文章:

iphone - nil 和 NULL 的区别

javascript - Date.getDate() 函数如何工作?

ruby-on-rails - 在 Ruby/Rails 中生成介于 2 个日期之间的商业周列表

javascript - JavaScript中有效的日期时间字符串是什么?

android - 使用 libgdx 在 Android/iOS 上开发 2D 游戏需要 Mac?

ios - 找不到 minizip/unzip.h 文件

android - 如何建立一个移动网站 - 基础知识,都是 CSS 吗?

iOS : How to get song id using echonest Api

ios - UICollectionView 单元格阴影

ios - CGRect 在 Swift 中改变原点