Date And Time Calculating Methods in Visual Basic 2008

This time I’m going to teach you how to calculate the date and the time on the present date in Visual Basic 2008. This method will calculate if you want to add months, days, hour, minutes, seconds and milliseconds to the present date. Then, the answers will appear in each field accurately. So, let’s begin: First, open the Visual Basic 2008, create a new Windows Application and do the Form just like this. First Form After that, go to the solution explorer and click the view code. View Code Then, create a sub procedure for the calculation of the months, day and time.
  1.    Private Sub Calculation_Methods()
  2.        
  3.  
  4.         'USE THE INSTANCE METHODS OF THE DATETTIME TYPE
  5.         'IT DEMONSTRATES THE DateTime.AddMonth, DateTime.AddDays , DateTime.AddHours
  6.         ' , DateTime.AddMinutes , DateTime.AddSeconds AND  DateTime.AddMilliseconds
  7.         Try
  8.             Dim date_Now As DateTime = DateAndTime.Now
  9.  
  10.             lblOriginalDate.Text = date_Now
  11.             txtAnsMonth.Text = date_Now.AddMonths(CInt(txtaddMonths.Text)).ToString
  12.             txtAnsDays.Text = date_Now.AddDays(CDbl(txtaddDays.Text)).ToString
  13.             txtAnsHours.Text = date_Now.AddHours(CDbl(txtAddHours.Text)).ToString
  14.             txtAnsMinutes.Text = date_Now.AddMinutes(CDbl(txtAddMinutes.Text)).ToString
  15.             TxtAnsSecond. Text = date_Now. AddMinutes (CDbl (txtAddSecond.Text)).ToString
  16.             txtAnsMilliseconds.Text = date_Now.AddMilliseconds(CDbl(txtAddMilliseconds.Text)).ToString
  17.  
  18.         Catch ex As Exception
  19.             MsgBox(Me.Text, ex.Message)
  20.         End Try
  21.     End Sub
After that, do the following code to set the value in the TextBox and call the method you have created to fire it on the first load.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         'SET THE VALUE IN THE TEXTBOX
  3.         For Each TXT As Control In Me.Controls
  4.             If TXT.GetType Is GetType(TextBox) Then
  5.                 TXT.Text = 7
  6.             End If
  7.         Next
  8.         'CALL THE METHOD TO CALCULATE FOR THE FIRST LOAD
  9.         Calculation_Methods()
  10.     End Sub
Do this following code for the click events of a "Calculate" Button.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         'CALL THE METHODS TO CALCULATE EVERYTIME THE BUTTON IS CLICKED
  3.         Calculation_Methods()
  4.     End Sub
Download the complete sourcecode and run it on you computer.

Add new comment