2012-01-22 77 views
0

目前,我有下面的代碼顯示倒計時的具體日期:如何顯示000:00:00:00格式的NSDate倒計時?

NSDate *date=[NSDate date]; 
int secondsNow=(int)[date timeIntervalSince1970]; 
NSDateFormatter *formatter=[[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"yyyy"]; 
NSString *LaunchBegin=[NSString stringWithFormat:@"20120126"]; // ENTER THE DATE OF LAUNCH HERE IN FORMAT YYYYMMDD 
[formatter setDateFormat:@"yyyyMMdd"]; 
NSDate *otherDate=[formatter dateFromString:LaunchBegin]; 
int secondsTarget=(int)[otherDate timeIntervalSince1970]; 
int differenceSeconds=secondsTarget-secondsNow; 
int days=(int)((double)differenceSeconds/(3600.0*24.00)); 
int diffDay=differenceSeconds-(days*3600*24); 
int hours=(int)((double)diffDay/3600.00); 
int diffMin=diffDay-(hours*3600); 
int minutes=(int)(diffMin/60.0); 
int seconds=diffMin-(minutes*60); 

CountdownLabel.text=[NSString stringWithFormat:@"%d:%d:%d:%d",days,hours,minutes,seconds]; // This is what I want to change to 000:00:00:00 

NSTimer *timer; 

timer = [NSTimer scheduledTimerWithTimeInterval: 0.8 
             target: self 
             selector: @selector(updateTimeToLaunch) 
             userInfo: nil 
             repeats: NO]; 

if(days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0) 
{ 

    UIImage *image = [UIImage imageNamed: @"end_countdown_highres.png"]; 
    [backing setImage:image]; 

    [facebook setHidden:NO]; 
    [twitter setHidden:NO]; 

} 

我怎樣才能使一個「000:00:00:00」的輸出格式,而不是目前的「0:0 :0:0「格式?

+0

由於Skippy指出,printf規範中的前導零表示應該使用零填充而不是空格使用零填充。非零數字表示字段的總寬度。 printf規範在多個地方在網絡上,你應該熟悉它。 –

回答

-1
[NSString stringWithFormat:@"%03d:%02d:%02d:%02d",days,hours,minutes,seconds]; 
+0

這不處理本地化。 –

相關問題