経過時間を時分秒の形式で表示

経過時間をh:mm:ssの形式で表示する例。

NSTimeInterval startTime = [[[NSDate alloc] init] timeIntervalSince1970]

//処理を実行 通常はstartTimeはインスタンス変数で定義し、↑と↓は別関数で実行する

int dt = (int)([[[NSDate alloc] init] timeIntervalSince1970] - startTime);
int s = dt % 60;
int m = (dt - s) / 60 % 60;
int h = (dt - s - m * 60) / 3600 % 3600;
if(h>9){//10時間を越えた場合は9:59:59と表示する。
    h = 9;
    m = 59;
    s = 59;
}
NSString *timeStr = [NSString stringWithFormat:@"%d:%02d:%02d", h, m, s];

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">