Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

C# / .NET - get current machine time in seconds

0 contributions
10 points

C# / .NET it is possible to take machine time in seconds in following way.

Getting second time with Stopwatch.GetTimestamp method example

public static class TimeUtils
{
	public static long GetSeconds()
	{
		double timestamp = Stopwatch.GetTimestamp();
		double seconds = timestamp / Stopwatch.Frequency;

		return (long)seconds;
	}
}

Example:

long t1 = TimeUtils.GetSeconds();

Thread.Sleep(1000);

long t2 = TimeUtils.GetSeconds();
long dt = t2 - t1;

Console.WriteLine(t2 + " - " + t1 + " = " + dt + " s");

Output:

200206 - 200205 = 1 s
Notes: 
  • Stopwatch.GetTimestamp returns time from timer mechanism - system up time.
  • The best application of this time could be measuring how much time took some operations.

References

  1. Stopwatch.GetTimestamp Method - Microsoft Docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more