Question

I am getting a NSString(in which i have a date)from database, I need to add days like suppose 10//12/15/45/60 days to the date, How can I do it.

when I am trying with this piece of code :

NSString *expiryDateStr = [NSString stringWithFormat:@"%@",[[assets valueForKeyPath:@"assetrewards.expirydate"] objectAtIndex:0]];
        NSLog(@" date %@",expiryDateStr);

        //DATE FORMATTER
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"dd/MM/yyyy"];
        NSDate *_date = [dateFormat dateFromString:expiryDateStr];
        [dateFormat setDateFormat:[NSString stringWithFormat:@"dd MMM yyyy HH:mm"]];
        [dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:3600*5]];
        NSLog(@"date format %@",dateFormat);
        NSLog(@"Date: %@", _date);

I am getting output like this :

date 07/10/2013
date format <NSDateFormatter: 0x9f9d4a0>
Date: 2013-10-06 18:30:00 +0000

the date is getting changed, I am not knowing where i am going wrong, if that is the correct date how to add the days to the date.

Was it helpful?

Solution

NSString *expiryDateStr = @"07/10/2013";
NSLog(@" date %@",expiryDateStr);
expiryDateStr=[expiryDateStr stringByAppendingString:@" 00:00:00 +000"];
//DATE FORMATTER
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss Z"];
NSDate *_date = [dateFormat dateFromString:expiryDateStr];
[dateFormat setDateFormat:[NSString stringWithFormat:@"dd MMM yyyy HH:mm"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit fromDate:_date];
NSInteger day = [components day]+1; // U can add as per your requirement


[components setDay:day];
NSDate *NextDate = [[NSCalendar currentCalendar] dateFromComponents:components];

NSLog(@"Next Date: %@", NextDate);

OTHER TIPS

you can and days into date like below...

set timezone like below...

  [dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];


    NSDate *dateAftertwenty = [NSDate dateWithTimeInterval:10*24*60*60 sinceDate:date]; //TimeInterval is of 10 days....you can set any number of days.24 is hours, 60 is minutes and 60 is seconds at last..

Let me know it is working or not!!!

Happy Coding!!!!

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSDate *_date = [dateFormat dateFromString:expiryDateStr];

is perfectly fine -- no problem. The "problem" is that

NSLog(@"Date: %@", _date);

will display the date as UTC/GMT, and hence (in India) a value five and a half hours earlier than the "real" date. This is not a problem -- when you format the date into a different string format (with the second incarnation of the date formatter) then it will come out correctly (so long as you DO NOT set the timezone for that version, if you didn't set it for the first).

To increment the date it's usually best to use NSDateComponents (more or less as shown by Paras) vs simply adding increments of 24 hours, in case there is any sort of "daylight savings time" transition in the date range.

UPDATE:

First setTimeZone to localTimeZone like bellow..

[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

Convert that String Date to NSDate with my bellow method..

-(NSDate *)convertStringToDate:(NSString *) date dateFormat:(NSString*)dateFormat{
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSDate *nowDate = [formatter setDateFormat:dateFormat];// set format here which format in string date
    date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
    nowDate = [formatter dateFromString:date];
    // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
    return nowDate;
}

and Use it like bellow..

NSDate *startDate = [self convertStringToDate:@"07/10/2013" dateFormate:@"dd/MM/yyyy"]; 

above set date formate which format is define in string

I have modified your code and I believe this is what you want:

NSString *expiryDateStr = @"07/10/2013";
NSLog(@" date %@",expiryDateStr);

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

[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *_date = [dateFormat dateFromString:expiryDateStr];
NSLog(@"date format %@",dateFormat);
NSLog(@"Date: %@", _date);

Output:
date 07/10/2013
date format
Date: 2013-10-07 00:00:00 +0000

Now changed the time zone in the code:

NSString *expiryDateStr = @"07/10/2013";
NSLog(@" date %@",expiryDateStr);

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

[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+5:30"]];
NSDate *_date = [dateFormat dateFromString:expiryDateStr];
NSLog(@"date format %@",dateFormat);
NSLog(@"Date: %@", _date);

And the output is:

date 07/10/2013
date format
Date: 2013-10-06 18:30:00 +0000

This is the output you were getting. This is because your time zone from your computer is being used.

EDIT(Number of days to add is denoted by days integer ):

NSString *expiryDateStr = @"07/10/2013";
NSLog(@" date %@",expiryDateStr);

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


[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *_date = [dateFormat dateFromString:expiryDateStr];
NSInteger days = 30;
_date = [_date dateByAddingTimeInterval:(60*60*24*days)];

NSLog(@"date format %@",dateFormat);
NSLog(@"Date: %@", _date);

Output of EDITed code:

date 07/10/2013
date format
Date: 2013-11-06 00:00:00 +0000

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top