- Home /
How to subtract one time from another?
Hello. What i want is when user press "Enter" button, we wright current time to variable "firstTime" and when we again press "Enter" we wright current time to variable "secondTime" and then subtract "firstTime" from "secondTime". So i want to know time between this two presses. Thanks in advice.
if( pressed Enter for first time?) {
firstTime = Time.time;
}
if( pressed Enter for second time?) {
secondTime = Time.time;
difference = secondTime - firstTime;
}
Thanks for answer. And how i can convert this to string format ("hh", "mm", "ss")?
I would recommend using TimeSpan for formatting the string
using System;
TimeSpan timeSpan = TimeSpan.FromSeconds(time);
string timeText = string.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.$$anonymous$$inutes, timeSpan.Seconds);
Answer by DeveshPandey · Sep 03, 2013 at 11:36 AM
Try this- float t = 0f;
void Update()
{
if(Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
{
float timeDiff = Time.time - t;
t = Time.time;
Debug.Log("Time after last pressed - "+ timeDiff +" Sec");
}
}
Thanks for answer. can i convert this to string format ("hh", "mm", "ss")?
no there is no any method to convert, but you can convert it by using your method. Please mark it as answer if its working.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113507im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collision Detection Issues 0 Answers
Lots of !IsFinite() Errors and FPS crawling to a halt - How to debug? 1 Answer
What is the best way to assign a duration to particles? 1 Answer
How to impliment a 10 minute day clock, and tie it to a directional light rotation. 4 Answers
How to do seveal actions like scale on an object one after another? 2 Answers