2012-07-24 87 views
1

如何獲取ios中兩個日期之間的天數名稱?獲取ios中兩個日期之間的天數名稱?

例子:

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

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

謝謝..

+0

查看['NSDateFormatter'](https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html),使用格式字符串@ 「dMy」將@「3-11-2012」和@「5-11-2012」從字符串轉換爲日期,然後使用@「dMy EEEE」將結果日期轉換爲字符串,包括星期幾。 – Rob 2012-07-24 05:35:41

+0

Ganesh ..不是天數...我想在兩個日期之間的日子的名字。 – Mani 2012-07-24 05:45:34

+0

通過這個 http://stackoverflow.com/questions/4575689/objective-c-calculating-the-number-of-days-between-two-dates – 2012-07-24 05:45:58

回答

11

試試這個,

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]; 

輸出爲

Friday, July 20, 2012 
Saturday, July 21, 2012 
Sunday, July 22, 2012 
Monday, July 23, 2012 
Tuesday, July 24, 2012 
+0

完美...謝謝... :) – Mani 2012-07-24 05:50:55

+0

Ooops我忘了發佈dayDifference對象... :) – Ankur 2012-07-24 05:53:43

0

這是你所需要的:

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]; 
1

入住此解決方案:

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

輸出是:

03-11-2012, Saturday 
04-11-2012, Sunday 
05-11-2012, Monday 
+0

謝謝傷痕... +1 – Mani 2012-07-24 05:54:42

0

下面是做到這一點的一種方法:使用NSCalendar給你

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

(和你的用戶)顯示一個更大的靈活性d操縱日期。

相關問題