NSDate在几天内倒计时

时间:2011-04-19 02:36:01

标签: iphone objective-c nsdate

我想创建一个截止日期的天数。我对几小时或几分钟不感兴趣,只是从当前日期到设定日期的天数。这就是我一直在尝试的:

    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)

但它不起作用,因为当前日期是以分钟和秒为单位的时间。

//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和格式化程序,但这是我对一堆互联网代码的结合。

提前致谢。

3 个答案:

答案 0 :(得分:5)

您可以通过这种方式计算当天的天数....

NSDate NewDate =这是比较日期;

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;

答案 1 :(得分:1)

您可以使用NSDateComponents来缩短时间:

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

然后通过仅指定day日期单位来获取天数差异(这可能在没有上述代码的情况下有效,我不确定):

int days = [[cal components:NSDayCalendarUnit fromDate:today  toDate:targetDate  options:0] day];

答案 2 :(得分:1)

使用以下代码

NSTimeInterval dateDiff = [eventDate timeIntervalSinceNow];
int countDown=trunc(dateDiff/(60*60*24));