- Home /
Laptime timer using TimeSpan - force milliseconds to 00 and not 000?
Hey guys, I'm using TimeSpan for a laptime timer in my racing game, which works great...but there's 1 little hiccup I can't seem to figure out, and that's for the Milliseconds section of the timer, which is always in this format: 000, and I want to force it to be: 00 instead.
Here's my code below:
private GUIText laptime;
void LateUpdate ()
{
// "timer" is being calculated in EventManager like this:
// timer = (Mathf.Round((Time.time - startTime) * 100.0f) / 100.0f);
laptime.text = "Laptime : " + SetTimeSpan( EventManager.timer );
}
public static string SetTimeSpan( float time )
{
// Create TimeSpan variable
System.TimeSpan timeSpan = System.TimeSpan.FromSeconds( time );
return string.Format( "{0:D2}:{1:D2}:{2:00}", timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds );
}
Is there a better way to create this type of timer? And how can I force it to always be in this format: 00:00:00 instead of 00:00:000?
Thanks for your time! Stephane
Answer by whydoidoit · Jun 16, 2012 at 07:39 PM
So that's a fair enough way to do it - though I would have used {0:00}:{1:00}{2:00} thought I guess D2 is working for you :) You just want to divide the milliseconds by 10.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Getting an object to spawn on a set time 1 Answer
Active Battle System implementation question. 1 Answer
how do I make a Time Based Score System? 1 Answer
Is there a timer that can start when game starts and stops when the game ends in Javascript? 1 Answer