Sunday, March 7, 2010

How To Get Number of Days or Date Difference between two Dates in C# Asp.Net




Please visit my new Web Site https://coderstechzone.com



In case of date time calculation from front end we need to find out the days difference between two given dates by user. Dates may come from dtpicker or from calendar control whatever we will use asp.net built in subtract method to identified the number of days or date difference between two dates. In this article i will show you to identify datediff between two dates in both C# and Vb.Net front end Language.












C# Code:
protected void Page_Load(object sender, EventArgs e)
    {
        DateTime dTFrom;
        DateTime dTo;
        string sFrom = "April 30, 2010";
        string sTo = "May 08, 2010";
        if (DateTime.TryParse(sFrom, out dTFrom) && DateTime.TryParse(sTo, out dTo))
        {
            TimeSpan TS = dTo.Subtract(dTFrom);
            int days_Diff = TS.Days;
            Response.Write("Day diference between two dates: "+days_Diff.ToString());
        } 
    }
VB.Net Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dTFrom As DateTime
        Dim dTo As DateTime
        Dim sFrom As String = "April 30, 2010"
        Dim sTo As String = "May 08, 2010"
        If DateTime.TryParse(sFrom, dTFrom) AndAlso DateTime.TryParse(sTo, dTo) Then
            Dim TS As TimeSpan = dTo.Subtract(dTFrom)
            Dim days_Diff As Integer = TS.Days
            Response.Write(days_Diff.ToString())
        End If
    End Sub

Hope now you can generate the date difference between two dates from a given date range.

0 comments:

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash