我有一个类似下面的数组
_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
我正在尝试从此处创建一个新数组,其中包含的内容如下所示
(特定生日的剩余天数) (我们将使用上述日期数组来计算当前日期,我们将查找剩余天数) _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 :(得分:0)
请尝试使用此。我认为这个可以帮到你。
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));
答案 1 :(得分: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循环中使用此功能,您将关闭剩余天数