我想创建一个截止日期的天数。我对几小时或几分钟不感兴趣,只是从当前日期到设定日期的天数。这就是我一直在尝试的:
I want to create a countdown of the number of days until a date. I'm not interested in hours or minutes, just the number of days from the current date until the set date. This is what I have been trying:
NSDate *date = [NSDate date]; NSString *dateString = [date description]; // this **dateString** string will have **"yyyy-MM-dd HH:mm:ss +0530"** // NSMutableString *mutString = dateString; NSArray *arr = [dateString componentSeperatedByString:@" "]; // arr will have [0] -> yyyy-MM-dd, [1] -> HH:mm:ss, [2] -> +0530 (time zone)但它不起作用,因为当前日期是在几分钟内给出时间和秒。
But it doesn't work because the current date is giving time in minutes and seconds.
//NSDate *currentDate = [[NSDate alloc] init]; NSString *start = [arr objectAtIndex:0];//[NSString stringWithFormat:@"%@", currentDate];//@"2011-11-05"; NSLog(@"%@",start); NSString *end = @"2011-6-03"; NSDateFormatter *f = [[NSDateFormatter alloc] init]; [f setDateFormat:@"yyyy-MM-dd"]; NSDate *startDate = [f dateFromString:start]; NSDate *endDate = [f dateFromString:end]; [f release]; NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0]; NSInteger days = [components day]; [gregorianCalendar release]; NSLog(@"%i", days);我真的不太了解这段代码,我不熟悉NSDate和格式化程序,但这是我的一堆互联网代码的组合。
I don't really understand much of this code, I'm not familar with NSDate and formatter, but this is my combination of a bunch of internet code.
提前致谢。
推荐答案你可以用这种方式计算当天的天数....
You can count the number of days from current day by this way....
NSDate NewDate =这里比较你的比较日期;
NSDate NewDate = Here Comes your compare date;
NSDateComponents * dateComp = [[NSDateComponents alloc] init];
NSDateComponents *dateComp = [[NSDateComponents alloc] init];
NSCalendar *Calander = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateFormatter *dateFormat=[[NSDateFormatter alloc] init]; NSDateComponents *comps=[[NSDateComponents alloc] init]; unsigned int unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; dateComp = [Calander components:unitFlags fromDate:[NSDate date]]; [dateFormat setDateFormat:@"dd"]; [comps setDay:[[dateFormat stringFromDate:[NSDate date]] intValue]]; [dateFormat setDateFormat:@"MM"]; [comps setMonth:[[dateFormat stringFromDate:[NSDate date]] intValue]]; [dateFormat setDateFormat:@"yyyy"]; [comps setYear:[[dateFormat stringFromDate:[NSDate date]] intValue]]; [dateFormat setDateFormat:@"HH"]; [comps setHour:05]; [dateFormat setDateFormat:@"mm"]; [comps setMinute:30]; NSDate *currentDate=[Calander dateFromComponents:comps]; //NSLog(@"Current Date is :- '%@'",currentDate); dateComp = [Calander components:unitFlags fromDate:newDate]; [dateFormat setDateFormat:@"dd"]; [comps setDay:[[dateFormat stringFromDate:newDate] intValue]]; [dateFormat setDateFormat:@"MM"]; [comps setMonth:[[dateFormat stringFromDate:newDate] intValue]]; [dateFormat setDateFormat:@"yyyy"]; [comps setYear:[[dateFormat stringFromDate:newDate] intValue]]; [dateFormat setDateFormat:@"HH"]; [comps setHour:05]; [dateFormat setDateFormat:@"mm"]; [comps setMinute:30]; NSDate *reminderDate=[Calander dateFromComponents:comps]; //NSLog(@"Current Date is :- '%@'",reminderDate); //NSLog(@"Current Date is :- '%@'",currentDate); NSTimeInterval ti = [reminderDate timeIntervalSinceDate:currentDate]; //NSLog(@"Time Interval is :- '%f'",ti); int days = ti/86400;更多推荐
NSDate在几天内倒计时
发布评论