How to compute Date Difference in VB.NET

In this article, i will introduce another some twist of the Date Function in VB.NET. I haven't posted for two days because of I was tasked to be a panelists for the Thesis Defense in Computer Engineering.But now I'm back! Hurray! haha :D So, now let's start this Date Difference tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add two DateTimePicker named DateTimePicker1 and DateTimePicker2 in the ToolBox. Then add a button named Button1 labeled as "Compute Date Difference". Take note that you have to place DateTimePicker2 above DatePicker1. You must design your layout like this: design 3. Now, put this code in Button1_Click. This will display the result of the DateDifference from the two DateTimePicker.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim firstDate As Date
  3.         Dim secondDate As Date
  4.  
  5.         firstDate = DateTimePicker1.Value
  6.         secondDate = DateTimePicker2.Value
  7.  
  8.         Dim diff2 As String = (secondDate - firstDate).TotalDays.ToString()
  9.  
  10.         MsgBox("The date difference is:" & " " & diff2.ToString())
  11.     End Sub

Explanation:

firstDate variable holds the value of DateTimePicker1 and secondDate variable holds the value of DateTimePicker2 as Date. Dim diff2 As String = (secondDate - firstDate).TotalDays.ToString() - this is the main syntax of this tutorial that computes the date difference between the two DateTimePicker. The variable diff2 holds the Date Difference between the secondDate and the FirstDate with their total days to minus. Then after being subtracted it is now converted into string. Then it will display the Date Difference of the two DateTimePicker.

Output:

date Download the source code below and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Comments

Good JOB bro:)(y) keep it up'!

Hi Donbermoy, Thanks a lot for your source codes. Would you please send me a library management system source code in vb 2005 express edition or vb 2008 which can be installed. I will be so grateful. It has given me a wonderful experience. Thank you

Add new comment