Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check difference in seconds between two times

Tags:

c#

People also ask

How do you calculate seconds between two times?

To get the total seconds between two times, you multiply the time difference by 86400, which is the number of seconds in one day (24 hours * 60 minutes * 60 seconds = 86400).

How do I calculate the time difference between two timestamps?

If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

How do I calculate seconds between two times in python?

Time Difference between two timestamps in Python Next, use the fromtimestamp() method to convert both start and end timestamps to datetime objects. We convert these timestamps to datetime because we want to subtract one timestamp from another. Next, use the total_seconds() method to get the difference in seconds.

How do you get time difference in seconds in typescript?

var startDate = new Date(); // Do your operations var endDate = new Date(); var seconds = (endDate. getTime() - startDate. getTime()) / 1000; Or even simpler (endDate - startDate) / 1000 as pointed out in the comments unless you're using typescript.


Assuming dateTime1 and dateTime2 are DateTime values:

var diffInSeconds = (dateTime1 - dateTime2).TotalSeconds;

In your case, you 'd use DateTime.Now as one of the values and the time in the list as the other. Be careful of the order, as the result can be negative if dateTime1 is earlier than dateTime2.


DateTime has a Subtract method and an overloaded - operator for just such an occasion:

DateTime now = DateTime.UtcNow;
TimeSpan difference = now.Subtract(otherTime); // could also write `now - otherTime`
if (difference.TotalSeconds > 5) { ... }

This version always returns the number of seconds difference as a positive number (same result as @freedeveloper's solution):

var seconds = System.Math.Abs((date1 - date2).TotalSeconds);

I use this to avoid negative interval.

var seconds = (date1< date2)? (date2- date1).TotalSeconds: (date1 - date2).TotalSeconds;