Z
Z
Zimaell2020-08-09 12:11:21
Unity
Zimaell, 2020-08-09 12:11:21

How to get difference in milliseconds from 2 dates?

Here is a method I wrote from examples on the network

private float GetTimeDiff(string stringDate){
        DateTime TimeNow = DateTime.UtcNow;
        TimeSpan diff = TimeNow - System.DateTime.Parse(stringDate);
        int seconds = diff.Seconds;
        int milliseconds = diff.Milliseconds;
        float returnDiff = (float)seconds + ((float)milliseconds / 1000);
        return returnDiff;
        }

.....
float diff = GetTimeDiff("2020-08-09 20:00:00");
Debug.Log(diff);
.....


in theory, the difference between time in milliseconds should be returned, but I look at the logs and see that there is a difference only until the end of the minute, and then again, that is, -0.002 -0.001 -59.999 -59.998 and so on ...
how to get a value to know how much specifically milliseconds before a certain time, or after it has passed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2020-08-09
@Zimaell

Read the description of the standard library . Seconds returns the number of seconds in the time interval, and you want the total duration in seconds (TotalSeconds) .
PS
Or milliseconds.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question