发现日期从数组日期 [英] finding out days remain from array of dates

查看:112
本文介绍了发现日期从数组日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面的数组

  _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 b $ b and until end like this ..



我使用下面的代码

  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 b $ b-39170,
-8612,
-9289,
-4454,
-8640,
9486,
-8994,
-8452,
-4454,
-2072,
-888 $ b-8315


解决方案

请尝试使用这个。我认为这可以帮助你。

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


i have one array something like below

_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"

)

this all are NSString

i am trying to make new array from this which contains days remaining something like below

(days remaining for particular birthdate) (we will compair current date with above date array and we will find out remaining days) _newarray( "125", "100", "65", and till end like this.. )

i am using below code for this

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

and this is what i am getting as output when i print _newarray plz help me thank you

_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"
)

解决方案

Please try to use this.I think this one may help you.

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

这篇关于发现日期从数组日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆