Search code examples
c#timesubtraction

How to calculate date and time before x hours and minutes?


I want to find exact date and time that was before x hours and x minutes from now.

For example - today is 2/21/2016 1:15 PM, and I want to know what was the time before 13 hours and 56 minutes or 73 hours and 34 minutes. What is the easiest way to do that in C#?

I've found this link, but don't know exactly how to use it.


Solution

  • Use AddHours() and AddMinutes(). Also works with negative values to go back in time.

    Example:

    DateTime pastDate = DateTime.Now.AddHours(-74).AddMinutes(-34);