I'm developing a system to calculate time difference. How can I compare two time (with date) and get the hours & minutes of the difference?
Example 1:
datetime1 = 1-Apr-2014 01:05:04 AM
datetime2 = 1-Apr-2014 02:05:04 AM
Results will be:
datetime2 - datetime1 = 01 Hours 00 Minutes
Example 2:
datetime1 = 1-Apr-2014 01:05:04 AM
datetime2 = 2-Apr-2014 02:15:04 AM
Results will be:
datetime2 - datetime1 = 25 Hours 10 Minutes
A negative value (for datetime1 > datetime2) would also be helpful for indicating that datetime1 is greater than datetime2
Example:
datetime1 = 2-Apr-2014 01:05:04 AM
datetime2 = 1-Apr-2014 01:05:04 AM
Results will be:
datetime2 - datetime1 = -24 Hours 00 Minutes
Thank you!
Solutions to my question is:
Another way, you can use
TotalHours
andMinutes
property ofTimeSpan
object resulted from subtracting twoDateTime
s :My recommendation would be to use something like
To extend this to seconds: