A Python to C# translation problem! time.time()

Hello everyone,

I’m just trying to translate a function from python to C# but couldn’t find the best equivalent. I hope I could use your helps in it.

The function time.time() in python to C# equivalent.

with regards,

Try DateTime.Now - DateTime.Now Property (System) | Microsoft Docs

Thank you Menno for your reply. Actually, I know about the properties of DateTime. But the problem is time.time() in Python counts the time from the first time it runs. As written here:

Two useful functions for time measurement are time.time and time.clock . time.time returns the time in seconds since the epoch, i.e., the point where the time starts.

But DateTime.Now uses that specific time of running the program! So should I just use now as a translated method from Python? or is there any closer?

I suppose it depends on what you want to use the result for.

There are multiple ways to handle dates and time in .NET (C#) - DateTime is one of them. DateTimeOffset is another, useful for calculating differences. If you want to time how long an operation takes, there is also the System.Diagnostics.Stopwatch class. These classes are all documented and a Google search away :wink:

1 Like

Thank you Menno. :slight_smile:

If you want the time in seconds since epoch you can use DateTimeOffset.Now.ToUnixTimeSeconds()

1 Like