Subtract two DateTimes
The following ASP.NET C# example code demonstrates to us how can we
subtract two DateTime objects programmatically at run time in an asp.net
application. The .NET framework's DateTime Subtract() method allows us to
subtract the specified time or duration from this instance. DateTime
Subtract() method is overloaded, those are Subtract(DateTime) and
Subtract(TimeSpan).
DateTime Subtract(DateTime) overloaded method subtracts the specified date and time from the instance. We need to pass a DateTime object to this method as a parameter. This method returns a TimeSpan value. The return TimeSpan object represents a time interval that is equal to the date and time of the instance minus the date and time of the parameter.
DateTime Subtract(TimeSpan) overloaded method subtracts the specified duration from this instance. This method is required passing a TimeSpan object as the parameter. The TimeSpan represents the time interval to subtract. This overloaded method returns a System.DateTime object which is equal to the date and time of instance minus the time interval of the parameter.
After getting the return TimeSpan value from DateTime Subtract(DateTime) method, we can convert the TimeSpan to total days, hours, minutes, seconds, etc. So we can get the total days, hours, and minutes difference between two DateTime objects.
DateTime Subtract(DateTime) overloaded method subtracts the specified date and time from the instance. We need to pass a DateTime object to this method as a parameter. This method returns a TimeSpan value. The return TimeSpan object represents a time interval that is equal to the date and time of the instance minus the date and time of the parameter.
DateTime Subtract(TimeSpan) overloaded method subtracts the specified duration from this instance. This method is required passing a TimeSpan object as the parameter. The TimeSpan represents the time interval to subtract. This overloaded method returns a System.DateTime object which is equal to the date and time of instance minus the time interval of the parameter.
After getting the return TimeSpan value from DateTime Subtract(DateTime) method, we can convert the TimeSpan to total days, hours, minutes, seconds, etc. So we can get the total days, hours, and minutes difference between two DateTime objects.
SubtractDateTime.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e) {
DateTime firstDateTime = DateTime.Now;
DateTime secondDateTime = DateTime.Now.AddDays(3);
TimeSpan dateTimeDifference;
dateTimeDifference = secondDateTime.Subtract(firstDateTime);
double totalHours = dateTimeDifference.TotalHours;
Label1.Text = "FirstDateTime: " + firstDateTime.ToString();
Label1.Text += "<br />SecondDateTime: " + secondDateTime.ToString();
Label1.Text += "<br /><br />Total Hours Difference Between Two DateTime Object: " + totalHours;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net date time example: how to subtract between two date time object</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Green">asp.net date time example: subtract date time</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Larger"
Font-Italic="true"
Font-Bold="true"
ForeColor="DodgerBlue"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Font-Bold="true"
ForeColor="DarkBlue"
OnClick="Button1_Click"
Text="Subtract Two DateTime Object"
/>
</div>
</form>
</body>
</html>