How to count a division of a second, in C#?

I am calculating a timecode feature in my programme. For this I need hours, minutes, seconds and an extra value that ticks 24 times per second.

My hours, minutes, seconds are:

public void GetTime()
{
    Hours = float.Parse(DateTime.Now.ToString("HH"));
    Minutes = float.Parse(DateTime.Now.ToString("mm"));
    Seconds = float.Parse(DateTime.Now.ToString("ss"));
    Frames = ???
}

How can I calculate the frames value? (without using a timer, if possible)

Thank you.

Jon Skeet
people
quotationmark

It sounds like you just want DateTime.Millisecond and then perform some arithmetic.

However:

  • You should only call DateTime.Now once, to avoid getting different values from different calls, leading to a very odd display
  • There's no need to perform formatting and parsing
  • There's no need to use non-integer types

Something like:

public void GetTime()
{
    var now = DateTime.Now;
    Hours = now.Hour;
    Minutes = now.Minute;
    Seconds = now.Second;
    Frames = (now.Millisecond * 24) / 1000;
}

Now that's the computation part. In order to update this regularly, you will need a timer or something similar. Also, due to the nature of system clocks - and timers - you shouldn't expect this code to give you a particularly smooth UI; it may feel a little jerky.

people

See more on this question at Stackoverflow