C# Realtime to int

Hi plase how i can do realtime to 2 other int ?
minutes and hours .
2 other ints one for minutes
one for hours

and next do to clock with

clock.Text = hour+":"+min.Tostring();

A simple way (because I’m still a noob and only do simple :)) is to have a variable called secondCounter and then in an update loop say secondCounter += Time.deltatime; . that will give u a seconds count. Then have a variable called minutes and a method in update that says if secondCounter >= 60 {minutes += 1 ; secondCounter =0;}. You can then of course do the same for hours with minutes .

If you want to give int and return results in Hours:Mins:Seconds then method will return results as you want.

 public string ToHourseMinutesSeconds(float seconds)
    {
        var s = seconds%60;
        var ms = s*1000;

        ms = ms%100;
        var m = (int) seconds/60;
        var h = m/60;

        var secondsDots = " ";
        secondsDots = ":";
        return ToDualDigit(m) + ":" + ToDualDigit((int) s);
    }
 private string ToDualDigit(int value)
    {
        if (value < 10)
        {
            return "0" + value;
        }
        return "" + value;
    }

Then you can call this just passing float values

var timer = ToHourseMinutesSeconds(30.0f);
clock.text = timer;

You should use the TimeSpan class. There’s a method for creating a TimeSpan object from seconds.

float seconds = 276;
TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);

Then you can get a formatted string representation of that object with its ToString() method or manually reference the Hours, Minutes and Seconds properties

Debug.Log(timeSpan.Minutes + "m " + timeSpan.Seconds + "s" );

(Not sure if using the ToString formatting is possible in Unity’s version of .NET)

If by realtime you mean the system time, use DateTime.Now.ToString() and pass a suitable formatting into the ToString method e.g.

string text = dateTime.ToString("MM/dd/yyyy HH:mm:ss.fff",
                                CultureInfo.InvariantCulture);

I found This Working

Clock.text = DateTime.Now.ToString("HH:mm");

:smiley: