2011-11-13 27 views
0

我正在嘗試在本週的某個特定日期進行倒計時。 我想顯示當前的工作日和週三剩餘的秒數。以秒爲單位倒數到特定的工作日

我可以對第一部分進行編碼,但即使在半天搜索解決方案後,我也無法處理第二部分。

這是我現在有:

- (void)viewDidLoad { 
    [countdownLabel setFont:[UIFont fontWithName:@"Helvetica" size:128.0]]; 
    countdownLabel.text = @"What day ist it?"; 
    [super viewDidLoad]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

-(void)updateLabel { 
    NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; 
    [theDateFormatter setDateFormat:@"EEEE"]; 
    countdownLabel.text = [theDateFormatter stringFromDate:[NSDate date]]; 
} 

回答

0

看看雙方的NSDate和NSDateFormatter類,他們是豐富的程序來做這樣的事情。

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 
NSDateFormatter * df = [NSDateFormatter dateFormatFromTemplate:@"yyyy-MM-dd-HH:mm" options:0 locale:usLocale]; 
NSDate * newyear = [df dateFromString:@"2012-01-01-00:00"]; 
NSTimeInterval seconds = [newyear timeIntervalSinceDate:[NSDate date]]; 

還沒有試圖編譯上述內容,但它應該給你秒。您應該能夠查看NSDateFormatter文檔以輕鬆找到一週中的某一天。

+0

謝謝,我已經做到了。我的問題是,我可以編碼到明年的新年倒計時。但是因爲我想在本週的某一天倒數到某一天,所以從本週的任何一天起,我都會迷失方向。 –