Monday, 20 October 2014

No of Days Between Two Dates By Using TimeSpan In Windows Application In C#.Net and VB.Net

10/20/2014 - By Pranav Singh 0

This article will show you how you can get the no of  date between two dates by using timespan in windows application using c#.net and VB.net.  In this I have used dateTimePicker.


So for this article first we will create a new windows application and add the dateTimePicker controls and button control in form. Now on button click event add the below code as shown below.

C#.Net
using System;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DateTime startDate = Convert.ToDateTime(dateTimePicker1.Value);
            DateTime EndDate = Convert.ToDateTime(dateTimePicker2.Value);
            /*Get time span between two days*/
            TimeSpan dateDiff = EndDate - startDate;
            /*Get no of days*/
            int diffInDays = dateDiff.Days;
            MessageBox.Show("Total no of days between tow days : " + diffInDays.ToString());
        }
    }
}

VB.Net
Imports System.Windows.Forms
Imports System.IO

Namespace WindowsFormsApplication9
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub
        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim startDate As DateTime = Convert.ToDateTime(dateTimePicker1.Value)
            Dim EndDate As DateTime = Convert.ToDateTime(dateTimePicker2.Value)
            'Get time span between two days

            Dim dateDiff As TimeSpan = EndDate - startDate
            'Get no of days

            Dim diffInDays As Integer = dateDiff.Days
            MessageBox.Show("Total no of days between tow days : " + diffInDays.ToString())
        End Sub
    End Class
End Namespace

In above code I have stored both control date into variable of datetime datatype. After that I have find the timespan and get the no of days .

Now we have done run the application and check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top