2012-11-20 107 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; 
}