ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Get Date difference in terms of Months using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="Sumeet"> Sumeet</a>,</p> <p>Refer the below sample code.</p> <p><span style="text-decoration: underline;">VB.Net</span></p> <pre class="brush: vb">Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Dim d1 As DateTime = Convert.ToDateTime(&#34;31 Dec 2015&#34;) Dim d2 As DateTime = DateTime.Now totalvalidityLabel.Text = MonthDifference(d1, d2).ToString() &#39; -17 totalvalidityLabel.Text = MonthDifference(d2, d1).ToString() &#39; 17 totalvalidityLabel.Text = MonthDifference1(d1, d2).ToString() &#39; 17 totalvalidityLabel.Text = MonthDifference1(d2, d1).ToString() &#39; 17 End Sub &#39; This returns a relative difference, meaning that if rValue is greater than lValue, then the return value will be negative. Public Shared Function MonthDifference(lValue As DateTime, rValue As DateTime) As Integer Return (lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year) End Function &#39;absolute difference. Public Shared Function MonthDifference1(lValue As DateTime, rValue As DateTime) As Integer Return Math.Abs((lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year)) End Function</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { DateTime d1 = Convert.ToDateTime(&#34;31 Dec 2015&#34;); DateTime d2 = DateTime.Now; totalvalidityLabel.Text = MonthDifference(d1, d2).ToString(); // -17 totalvalidityLabel.Text = MonthDifference(d2, d1).ToString(); // 17 totalvalidityLabel.Text = MonthDifference1(d1, d2).ToString(); // 17 totalvalidityLabel.Text = MonthDifference1(d2, d1).ToString(); // 17 } // This returns a relative difference, meaning that if rValue is greater than lValue, then the return value will be negative. public static int MonthDifference(DateTime lValue, DateTime rValue) { return (lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year); } //absolute difference. public static int MonthDifference1(DateTime lValue, DateTime rValue) { return Math.Abs((lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year)); }</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/134095/Get-Date-difference-in-terms-of-Months-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/134095/Get-Date-difference-in-terms-of-Months-using-C-and-VBNet-in-ASPNet/Tue, 23 May 2017 05:36:07 GMT