# Getting minute to increment once seconds reach 59

I’m using the following code to make a timer .

``````void Update () {

time += Time.deltaTime;

//Divide the guiTime by sixty to get the minutes.
seconds = time % 60;
minutes = time/ 60;//Use the euclidean division for the seconds.
//		float fraction = (time * 100) % 100;
//update the label value
if (minutes <= 1.0) {
if (seconds < 10) {
timerLabel.text = string.Format (" {0:0}", seconds);
} else {
timerLabel.text = string.Format (" {0:00}", seconds);
}
} else if (minutes >= 1.0 && minutes <= 10.0) {
timerLabel.text = string.Format (" {0:0}:{1:00}", Mathf.Floor (minutes), seconds);
}
else  {
timerLabel.text = string.Format (" {0:00}:{1:00}", Mathf.Floor (minutes), seconds);

}
}
``````

and than i get the time from this script in other script by using this method .

``````public string GetCurrentTime()
{
if (minutes <= 1.0) {
if (seconds < 10) {
return string.Format (" {0:0}", seconds) +" Seconds" ;
} else {
return string.Format (" {0:00}", seconds) +  " Seconds" ;
}
}
else if (minutes >= 1.0 && minutes <= 10.0){
if (seconds <= 10) {
return string.Format ("{0:0}", Mathf.Floor (minutes)) + " Minutes" + "
``````

" + string.Format (“{0:0}”, seconds) + " Seconds";
} else {
return string.Format (“{0:0}”, Mathf.Floor (minutes)) + " Minutes" + "
" + string.Format (“{0:00}”, seconds) + " Seconds";
}
} else {
if (seconds <= 10) {
return string.Format (“{0:0}”, Mathf.Floor (minutes))+ " Minutes"+"
“+string.Format (”{0:0}“, seconds) +” Seconds";
} else {
return string.Format (“{0:0}”, Mathf.Floor (minutes))+ " Minutes"+"
“+string.Format (”{0:00}“, seconds)+ " Seconds” ;
}
}

``````}

}
``````

But the problem is timer increments this way it gets to like 1:59, than 1:60 and than 2:00 what i want is to emit the 1:60 and , is there a better way to format what i 'm formatting for my timer.

You can reduce your Update() function to three lines using the TimeSpan class.

``````void Update () {
time += Time.deltaTime;
TimeSpan ts = TimeSpan.FromSeconds(time);
timerLabel.text = string.Format("{0:D2}:{1:D2}", ts.Minutes, ts.Seconds);
}
``````