Digital clock/timer with specific starting time.

Hello. I am trying to make a digital clock and now it is accessing the time on my computer, but how could i change the time to a specific one. I need it to be starting from 00:00:00 (midnight)

This is what i have now.

public class Clock : MonoBehaviour
{
    private TMP_Text textClock;
 
    // Start is called before the first frame update
    void Start()
    {
        textClock = this.GetComponent<TMP_Text>();

    }

    // Update is called once per frame
    void Update()
    {

        DateTime time = DateTime.Now;
      
        string hour = LeadingZero(time.Hour );
        string minute = LeadingZero(time.Minute);
        string second = LeadingZero(time.Second);

        textClock.text = hour + ":" + minute + ":" + second;
    }

    string LeadingZero(int n)
    {
        return n.ToString().PadLeft(2, '0');
    }
  
  
}

In start() get the current time and record it to a variable.
In Update, as you do now get current time again, but now get the timespan difference between the recorded start time and the time now. Then add that timespan difference to a datetime of your arbitrary start time and you got your adjusted time.

1 Like