1

以下のような生年月日を含む配列があります。

 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では、2013 年の日付ではなく「20/02/2014」である必要があります。問題の原因は何ですか?どうすれば解決できますか?

4

4 に答える 4

2

私はあなたのコードにいくつかの変更を加えましたが、あなたが望むように動作しています.

私のコードでは、現在の日付の昇順形式である日付を比較しました。条件を満たしていれば、DateFormatter "yyyy" によって、一致した日付から YEAR を取得しました。次に、この年を 1 増やして、古い日付の「2013 年2 月20 日」を「2014 年2 月 20 日」に置き換えます。

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);
}

これは完璧に機能しているようですので、お役に立てば幸いです。

于 2013-03-13T12:01:55.893 に答える
1

これを使ってみてください。これが役立つと思います

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

それ以外の場合は、この 1 つのコードを使用できます。

   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"

   }
于 2013-03-13T11:53:41.780 に答える
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);
于 2013-03-13T12:19:18.313 に答える
0

Compare your array date to today's date with NSDate compare function. Here are the details:

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}

If it results NSOrderedAscending, then it means array date is earlier than today's date.

So for that date, update year incremented by one using NSDateComponents:

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

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

Or you can use NSDate function itself:

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

But second option is not full-proof - because of leap year problem.

Hope this two options help you, for solving your issue.

于 2013-03-13T11:54:46.330 に答える