Check if timestamp is X seconds old in C#?

All we need is an easy explanation of the problem, so here it is.

I have two unix timestamps old and new and I want to check if 30 seconds has passed between them. I want to check if oldTs is 30 seconds old or not. Basically if oldTs is <= 30 second old then I want to return true otherwise I want to return false.

long oldTs = long.Parse(processData.timestamp);
long currentTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

return currentTimestamp >= (oldTs + TimeSpan.FromSeconds(30));

I tried above code but I get an error as –

Operator '+' cannot be applied to operands of type 'long' and 'TimeSpan' 

What is wrong I am doing here?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Set both variables in seconds. You have to simply to oldTS + 30.

You can get the the unix timestamp like this

long unixTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

Method 2

This return a TimeSpan data type.


You can try get the milliseconds of it.


I hope it helps. Happy coding.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

Leave a Reply