ARTICLE

How To calculate Number Of Days Between Two Dates

Posted by Alok Pandey Articles | Windows Forms VB.NET August 23, 2011
Here, we will see how To calculate Number Of Days Between Two Dates.
Download Files:
 
Reader Level:

Introduction

This is Simple Way Of calculating Number Of Days Between Two Dates . User Can Select Two

Dates By  DateTimePicker and He Can Calculate Number Of Days. For This, Take a Window Form ,

Two Labels , Two DateTimePicker and a Button. Then Set the Properties Of These Controls as ,

Label

Set Its Name as Start Date and End Date by it's Text Property.

DateTimePicker

Set its Format Property as Short.

Button

Set its Name as Calculate  By its Text Property. Then Form Will Look Like below figure1.

image1.gif

Figure1

After This Write The Following Code On Double Click Of Calculate Button.

Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click

        If True Then

            Dim dt1 As DateTime = Convert.ToDateTime(DateTimePicker1.Text)

            Dim dt2 As DateTime = Convert.ToDateTime(DateTimePicker2.Text)

            Dim ts As TimeSpan = dt2.Subtract(dt1)

            If Convert.ToInt32(ts.Days) >= 0 Then

                MessageBox.Show("Total Days are " & Convert.ToInt32(ts.Days))

            Else

                MessageBox.Show("Invalid Input")

            End If

        End If

    End Sub

Then, Select Two Dates And Click Calculate Button, The Result Will Look Like This.

image2.gif

Figure2

If User Does Wrong Entry ,Means If End Date Comes Before Start Date , Then Error Message

Will Show as "Invalid Input". Like as below.

 image3.gif

Figure3

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor