1

I want to retrieve the last date of given month in iphone app. Can anybody tell how can I achieve this? I saw some solution on stackoverflow , but they just giving last DAY of month.

Thanks.

UPDATE:

    +(NSString*)getLastDateOfMonth:(NSString *)monthYearString {

    //NSString *inputDate = [NSString stringWithFormat:@"%@ %@",@"01",monthYearString];
    NSString *inputDate = [NSString stringWithFormat:@"%@ %@",@"01",@"May 2012"];
    NSLog(@"Input date:- %@",inputDate);

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd MMM yyyy"];
    NSDate *date = [dateFormatter dateFromString:inputDate];

    NSCalendar *gregCalendar=[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

    NSDateComponents *components=[gregCalendar components:NSMonthCalendarUnit|NSYearCalendarUnit fromDate:date];
    NSInteger month=[components month];
    NSInteger year=[components year];

    if (month==12) {
        [components setYear:year+1];
        [components setMonth:1];
    }
    else {
        [components setMonth:month+1];
    }
    [components setDay:1];

    NSDate *lastDate = [[gregCalendar dateFromComponents:components] dateByAddingTimeInterval:0];

    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
    [outputFormatter setDateFormat:@"MM/dd/yyyy"];
    NSString *lastDateOfMonth = [outputFormatter stringFromDate:lastDate];

    return lastDateOfMonth;

}

If I use this code it showing me wrong output in console as follows

    Input date:- 01 May 2012
LAst DAte in Month:- 06/01/2012
iOSAppDev
  • 2,755
  • 4
  • 39
  • 77

1 Answers1

4

You can do that by using this function :-

 NSDate currDate=[NSDate date];

NSDate *getLastDateMonth(NSDate *currDate)
{

    NSCalendar *gregCalendar=[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

    NSDateComponents *components=[gregCalendar components:NSMonthCalendarUnit|NSYearCalendarUnit fromDate:currDate];
    NSInteger month=[components month];
    NSInteger year=[components year];

    if (month==12) {
        [components setYear:year+1];
        [components setMonth:1];
    }
    else {
        [components setMonth:month+1];
    }
    [components setDay:1];

    return [[gregCalendar dateFromComponents:components] dateByAddingTimeInterval:0];
    }
Milap Kundalia
  • 1,566
  • 1
  • 16
  • 24
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
  • If I use above function I am getting wrong date. I call this function like this NSLog(@"LAst DAte in Month:- %@",[Common getLastDateOfMonth:[NSDate date]]);The output in my console is "LAst DAte in Month:- 2012-05-30 18:30:00 +0000" – iOSAppDev May 21 '12 at 04:42
  • remove -86400 from return and just add 0 – Abhishek Singh May 21 '12 at 08:37
  • Thanks for answer. But if I use code from my UPDATE (Please see UPDATE to my question) then its showing me wrong result. Can yoou help me out. Thanks in advance. – iOSAppDev May 21 '12 at 09:28
  • if you have to get the string you will have to use -86400 instead of 0 as it increases by 1 – Abhishek Singh May 21 '12 at 09:53