- (int) daysToDate:(NSDate*) endDate
{
//dates needed to be reset to represent only yyyy-mm-dd to get correct number of days between two days.
NSDateFormatter *temp = [[NSDateFormatter alloc] init];
[temp setDateFormat:@"yyyy-MM-dd"];
NSDate *stDt = [temp dateFromString:[temp stringFromDate:[NSDate date]]];
NSDate *endDt = [temp dateFromString:[temp stringFromDate:endDate]];
[temp release];
unsigned int unitFlags = NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:endDt toDate:stDt options:0];
int days = [comps day];
[gregorian release];
return days;
}
Call the function:
NSString *str =[NSString stringWithFormat:@"%@ 00:00:00",[strdate substringToIndex:10]];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dd = [df dateFromString:str];
int d = [self daysToDate:dd];
Note: PASS date in yyyy-MM-dd HH:mm:ss formate
where date time should be 00:00:00
so date should be as 2010-12-02 00:00:00
thanks
{
//dates needed to be reset to represent only yyyy-mm-dd to get correct number of days between two days.
NSDateFormatter *temp = [[NSDateFormatter alloc] init];
[temp setDateFormat:@"yyyy-MM-dd"];
NSDate *stDt = [temp dateFromString:[temp stringFromDate:[NSDate date]]];
NSDate *endDt = [temp dateFromString:[temp stringFromDate:endDate]];
[temp release];
unsigned int unitFlags = NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:endDt toDate:stDt options:0];
int days = [comps day];
[gregorian release];
return days;
}
Call the function:
NSString *str =[NSString stringWithFormat:@"%@ 00:00:00",[strdate substringToIndex:10]];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dd = [df dateFromString:str];
int d = [self daysToDate:dd];
Note: PASS date in yyyy-MM-dd HH:mm:ss formate
where date time should be 00:00:00
so date should be as 2010-12-02 00:00:00
thanks
No comments:
Post a Comment