- Home /
System.DateTime truncate Milliseconds
I've been trying to make a timer that displays hours, minutes and seconds. I have been using this:
string.Format("{0:HH:mm:ss}", startTime - System.DateTime.Now);
This should only display what I want but it also displays milliseconds. Does anybody know why?
There are many ways to achieve this, but first you need to do
System.DateTime.Now - startTime
ins$$anonymous$$d
startTime - System.DateTime.Now
But you can easily solve your problem by doing this
TimeSpan elapsedTime = DateTime.Now - startTime;
text.text = string.Format("Time : {0:D2}:{1:D2}:{2:D2}", elapsedTime.Hours, elapsedTime.$$anonymous$$inutes, elapsedTime.Seconds);
Answer by RobAnthem · Dec 08, 2016 at 06:47 AM
That's really weird since you aren't referencing the t value, but DateTime doesn't need formatting, you can declare the format as a basic .ToString("format here") .Net has a huge documentation about DateTime formatting.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to convert timer to minutes, seconds and cents 3 Answers
how to Subtract string from TimeSpan 0 Answers
String format to show float as time 3 Answers