NSDate countdown in days
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(@"%@",star开发者_JAVA百科t);
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);
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.
Thanks in advance.
You can count the number of days from current day by this way....
NSDate NewDate = Here Comes your compare date;
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;
You can use NSDateComponents
to trim off the time:
NSDate* targetDate = ...
NSDate* today = [NSDate date]
NSCalendar* cal = [NSCalendar currentCalendar]
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
targetDate = [cal dateFromComponents:[cal components:unitFlags fromDate:targetDate]];
today = [cal dateFromComponents:[cal components:unitFlags fromDate:today]];
And then grab the differences in days by specifying only the day
date unit (this might work without the code above, I'm not sure):
int days = [[cal components:NSDayCalendarUnit fromDate:today toDate:targetDate options:0] day];
Use the following code,
NSTimeInterval dateDiff = [eventDate timeIntervalSinceNow];
int countDown=trunc(dateDiff/(60*60*24));
精彩评论