2011-06-30 26 views

回答

4

這將返回當前日期時間

[NSDate date]; 

現在你可以使用的NSTimer來獲取時間每隔一秒並顯示它。

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(displayTime) userInfo:nil repeats:NO]; 
//Time Interval is one second 
-(void) displayTime 
{ 

    NSDate *currentDate = [NSDate date]; 
    NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease]; 
    timeFormatter.dateFormat = @"HH:mm:ss a"; 
    NSString *dateString = [timeFormatter stringFromDate: currentDate]; 
    yourlabel.text = dateString; 
    [timeFormatter release] 
} 
+0

感謝您的代碼運行良好,我還有一個問題,如何顯示AM/PM的時間。請幫忙! – Santusura8

+0

timeFormatter.dateFormat = @「HH:mm:ss a」; – iPrabu

+0

感謝您的快速回復! – Santusura8