1

How to get name of days between two dates in ios?

Example:

Input:
 Start Date: 3-11-2012 
 End date:   5-11-2012

Output:
 3-11-2012 Wednesday
 4-11-2012 Thursday
 5-11-2012 Friday

Thanks..

Mani
  • 1,841
  • 15
  • 29
  • Check out [`NSDateFormatter`](https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html), using format string of @"d-M-y" for converting @"3-11-2012" and @"5-11-2012" from strings to dates, and then using @"d-M-y EEEE" to convert the resulting dates back to strings including the day of the week. – Rob Jul 24 '12 at 05:35
  • Ganesh.. not number of days... i wanna name of days between two dates. – Mani Jul 24 '12 at 05:45
  • go through this http://stackoverflow.com/questions/4575689/objective-c-calculating-the-number-of-days-between-two-dates – ganesh manoj Jul 24 '12 at 05:45
  • It's not clear what you mean by "days between two dates". Your example shows only the "mid-point" date; is that your desired outcome? What if the start and end dates aren't the same day of month? – Richard Jul 24 '12 at 05:51
  • Richard .. now problem solve.. ankur's code is working perfectly .. thankyou. and thanks to all... – Mani Jul 24 '12 at 05:52

4 Answers4

11

try this,

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *startDate = [df dateFromString:@"2012-07-20"]; // your start date
    NSDate *endDate = [NSDate date]; // your end date
    NSDateComponents *dayDifference = [[NSDateComponents alloc] init];

    NSMutableArray *dates = [[[NSMutableArray alloc] init] autorelease];
    NSUInteger dayOffset = 1;
    NSDate *nextDate = startDate;
    do {
        [dates addObject:nextDate];

        [dayDifference setDay:dayOffset++];
        NSDate *d = [[NSCalendar currentCalendar] dateByAddingComponents:dayDifference toDate:startDate options:0];
        nextDate = d;
    } while([nextDate compare:endDate] == NSOrderedAscending);

    [df setDateStyle:NSDateFormatterFullStyle];
    for (NSDate *date in dates) {
        NSLog(@"%@", [df stringFromDate:date]);
    }
    [df release];

Output is :

Friday, July 20, 2012
Saturday, July 21, 2012
Sunday, July 22, 2012
Monday, July 23, 2012
Tuesday, July 24, 2012
Ankur
  • 5,086
  • 19
  • 37
  • 62
1

Check this solution:

NSDateComponents *components;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];

NSDate *startDate = [dateFormat dateFromString:@"3-11-2012"];
NSDate *endDate = [dateFormat dateFromString:@"5-11-2012"];

[dateFormat setDateFormat:@"dd-MM-yyyy, EEEE"];

components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0];

int days = [components day];

for (int x = 0; x <= days; x++) {
    NSLog(@"%@", [dateFormat stringFromDate:startDate]);
    startDate = [startDate dateByAddingTimeInterval:(60 * 60 * 24)];
}

The output is:

03-11-2012, Saturday
04-11-2012, Sunday
05-11-2012, Monday
Scar
  • 3,460
  • 3
  • 26
  • 51
1

Here is one way to do it:

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:2012];
[dateComponents setMonth:11];
[dateComponents setDay:03];

NSDate *date1 = [gregorianCalendar dateFromComponents:dateComponents];

[dateComponents setYear:2012];
[dateComponents setMonth:11];
[dateComponents setDay:05];

NSDate *date2 = [gregorianCalendar dateFromComponents:dateComponents];

NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:@"dd-MM-yyyy EEEE"];
NSDate *dayDate;

dayDate = date1;
NSLog(@"%@", [outputDateFormatter stringFromDate:dayDate]);

do {
    dayDate = [dayDate dateByAddingTimeInterval:86400];
    NSLog(@"%@", [outputDateFormatter stringFromDate:dayDate]);

} while ([dayDate compare:date2]==NSOrderedAscending);

using NSCalendar gives you (and your user) greater flexibility for displaying and manipulating dates.

Andrei G.
  • 1,710
  • 1
  • 14
  • 23
0

This is what you need:

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"mm-dd-yyyy"];
NSDate *inputDate = [inputFormatter dateFromString:@"3-11-2012"];

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"mm-dd-yyyy EEEE"];
NSString *output = [outputFormatter stringFromDate:inputDate];

NSLog(@"%@",output);

[inputFormatter release];
[outputFormatter release];
Stavash
  • 14,244
  • 5
  • 52
  • 80