2013-03-13 292 views
-3

的陣列我有一個數組類似下面找出剩餘天數從日期

_combinedBirthdates(
"03/12/2013", 
"03/12/2013", 
"08/13/1990", 
"12/09/1989", 
"02/06", 
"09/08", 
"03/02/1990", 
"08/22/1989", 
"03/02", 
"05/13", 
"10/16", 
"07/08", 
"08/31/1990", 
"04/14/1992", 
"12/15/1905", 
"08/14/1989", 
"10/07/1987", 
"07/25", 
"07/17/1989", 
"03/24/1987", 
"07/28/1988", 
"01/21/1990", 
"10/13" 

這一切都是NSString的

我想從這個,這使得新的數組包含天剩下下面的東西

(特定出生日期剩餘的天數) (我們將比較當前日期與上面的日期數組,我們將f IND出剩餘天數) _newarray( 「125」, 「100」, 「65」,和 直到結束這樣的.. )

我使用下面碼本

unsigned int unitFlags = NSDayCalendarUnit; 
NSCalendar *currCalendar = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

[dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
NSLog(@" currCalendar%@",currCalendar); 

NSMutableArray * _newarray = [[NSMutableArray alloc] init]; 
for (NSString * str in _combinedBirthdates){ 

    NSDate * toDate = [dateFormatter dateFromString:str]; 
    NSDateComponents *daysInfo = [currCalendar components:unitFlags fromDate:[NSDate date] toDate:toDate options:0]; 
    int days = [daysInfo day]; 
    [_newarray addObject:[NSString stringWithFormat:@"%d",days]]; 
} 
NSLog(@" _newarray%@",_newarray); 

這就是我得到的輸出當我打印_newarray plz幫助我感謝你

_newlymadeArray(
"-1", 
"-1", 
"-8248", 
"-8495", 
"-4454", 
"-4454", 
"-8412", 
"-8604", 
"-4454", 
"-4454", 
"-4454", 
"-4454", 
"-8230", 
"-7638", 
"-39170", 
"-8612", 
"-9289", 
"-4454", 
"-8640", 
"-9486", 
"-8994", 
"-8452", 
"-4454", 
"-2072", 
"-888", 
"-8315" 
) 
+0

我不能讓重量ü真正想要的: ( – iPatel 2013-03-13 05:08:40

+0

天重新爲特定的出生日期 – 2013-03-13 05:10:39

+1

我認爲這是解決在這個問題'http:// stackoverflow.com/questions/15358210/how-to-make-another-array-containing-number-of-days-remain' – 2013-03-13 05:11:55

回答

0

請儘量使用this.I覺得這個可以幫助你。

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

    NSString *str = [tempFormatter stringFromDate:@"yourStartDate"]; 
    NSDate *startdate = [tempFormatter dateFromString:str]; 

    str = [tempFormatter stringFromDate:@"yourEndDate"]; 
    NSDate *endDate = [tempFormatter dateFromString:str]; 

    int i = [startdate timeIntervalSince1970]; 
    int j = [toDate timeIntervalSince1970]; 

    double X = j-i; 

    int days=(int)((double)X/(3600.0*24.00)); 
+0

當我嘗試打印INT天,那麼這是我得到單步直到退出功能objc_msgSend, 它沒有行號信息。 – 2013-03-13 06:10:23

+0

非常感謝你,我從你的風格得到了答案 – 2013-03-13 06:18:36

+0

你最歡迎親愛的... – 2013-03-13 07:40:28

0

使用下面的代碼來比較兩個日期同時通過日期相同的格式

NSDateFormatter *dateformat = [[NSDateFormatter alloc]init]; 
[dateformat setDateFormat:@"dd/MM/yyyy"]; 
NSString *StrDate1 = [dateformat stringFromDate:[NSDate date]]; 
NSString *StrDate2 = [dateformat stringFromDate:date2]; 
NSDate *firstDate = [dateformat dateFromString:StrDate1]; 
NSDate *secondDate = [dateformat dateFromString:StrDate2]; 
NSTimeInterval lastDiff = [firstDate timeIntervalSinceNow]; 
NSTimeInterval todaysDiff = [secondDate timeIntervalSinceNow]; 
NSTimeInterval dateDiff = todaysDiff - lastDiff; 
int day = dateDiff/(3600*24); 

// days are remaining 

使用這種在for循環,你會門剩下的日子

+0

當我嘗試詮釋天天然後這是我得到單步直到退出功能objc_msgSend, 其中沒有行號信息。 – 2013-03-13 06:14:15

+0

非常感謝你我回復了你的風格 – 2013-03-13 06:20:17

+0

你是最受歡迎的 – Pratik 2013-03-13 06:32:05