2012-11-20 58 views
0

试图创建一个倒数计时器达到某个日期,但我希望它显示的东西在几天,而不是月,天(喜欢,2个月的2Days)我宁愿只是让它显示实际天数像“62天“我应该在代码中更改什么?谢谢。如何获得倒数计时器以显示倒数天数?

下面是我用于CountDown的代码。

#import "CountdownViewController.h" 

@interface CountdownViewController() 

@end 

@implementation CountdownViewController; 

-(void)updateLabel; 
{ 
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    char units = NSDayCalendarUnit; 
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0]; 
    [dateLabel setText:[NSString stringWithFormat:@"%dDays", [components day]]]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    destinationDate = [NSDate dateWithTimeIntervalSince1970:1356393600]; 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

问题是,它只是显示我的日子部分。我的代码打以上到达的代码,但它曾经是这样

-(void)updateLabel; 
{ 
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    char units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; 
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0]; 
    [dateLabel setText:[NSString stringWithFormat:@"%d %d %d %d %d", [components month], [components day], [components hour], [components minute], [components second]]]; 
} 

回答

0

试试这个方法从两个日期得到天数。

- (int)daysBetweenDates:(NSDate *)dt1:(NSDate *)dt2 { 
int numDays; 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSUInteger unitFlags = NSDayCalendarUnit; 
NSDateComponents *components = [gregorian components:unitFlags fromDate:dt1 toDate:dt2 options:0]; 
numDays = [components day]; 
[gregorian release]; 
return numDays; 
} 

使用这种像波纹管..

-(void)updateLabel; 
{ 
    int tempdays = [self daysBetweenDates:destinationDate :[NSDate date]]; 
    [dateLabel setText:[NSString stringWithFormat:@"%dDays", tempdays]]; 
} 
0

之前,你的标签供应值,在这种情况下,dateLabel;你应该串连您的字符串,包括年,月,日,或不管它是什么......

2

只需将以下两种方法复制到您的类中,然后调用viewDidLoad方法启动计时器。您需要更改getCountdownDate方法所需的日期:

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(getCountdownDate) userInfo:nil repeats: YES]; 

的方法是:

#pragma mark -- 
#pragma mark CountDownTimmer takes one date input 

-(void)getCountdownDate 
{ 
    NSString *date = @"2013-09-09 20:30:00"; 
    NSDate *toDate = [self formatADateFromString:date]; 
    NSLog(@"Date: %@", toDate); 

    NSString *remainingCountDown = [self countDownTimerToSpecificDate:toDate]; 
    NSLog(@"%@", remainingCountDown); 
} 



-(NSString *)countDownTimerToSpecificDate:(NSDate*)toDateParameter 
{ 
    NSDate *toDate = toDateParameter; 
    NSDate *currentDate = [NSDate date]; 
    NSLog(@"To Date: %@, Current Date: %@", toDate, currentDate); 

    int units; 
    int months, days, hour, minit, second_t; 


    NSDateComponents *components; 
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

    units = NSDayCalendarUnit; 
    components = [calender components:units fromDate:currentDate toDate:toDate options:0]; 
    days = [components day]; 
    days = days%30; 

    units = NSMonthCalendarUnit; 
    components = [calender components:units fromDate:currentDate toDate:toDate options:0]; 
    months = [components day]; 
    months = months%12; 


    //int totalSec = [components second]; 
    //int month = [components month]; 
    units = NSHourCalendarUnit; 
    components = [calender components:units fromDate:currentDate toDate:toDate options:0]; 
    hour = [components hour]; 
    hour = hour%24; 

    units = NSMinuteCalendarUnit; 
    components = [calender components:units fromDate:currentDate toDate:toDate options:0]; 
    minit = [components minute]; 
    minit = minit%60; 

    units = NSSecondCalendarUnit; 
    components = [calender components:units fromDate:currentDate toDate:toDate options:0]; 
    second_t = [components second]; 
    second_t = second_t%60; 

    NSString *returnString = [NSString stringWithFormat:@"%d Months, %d Days, %d Hours, %d Minitues, %d Seconds",months, days, hour, minit, second_t]; 

    //NSLog(@"%@", returnString); 
    return returnString; 
}