Date And Time Calculating Methods in Visual Basic 2008
Submitted by janobe on Monday, May 12, 2014 - 09:01.
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.
After that, go to the solution explorer and click the view code.
Then, create a sub procedure for the calculation of the months, day and time.
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.
Do this following code for the
Download the complete sourcecode and run it on you computer.
- Private Sub Calculation_Methods()
- 'USE THE INSTANCE METHODS OF THE DATETTIME TYPE
- 'IT DEMONSTRATES THE DateTime.AddMonth, DateTime.AddDays , DateTime.AddHours
- ' , DateTime.AddMinutes , DateTime.AddSeconds AND DateTime.AddMilliseconds
- Try
- Dim date_Now As DateTime = DateAndTime.Now
- lblOriginalDate.Text = date_Now
- txtAnsMonth.Text = date_Now.AddMonths(CInt(txtaddMonths.Text)).ToString
- txtAnsDays.Text = date_Now.AddDays(CDbl(txtaddDays.Text)).ToString
- txtAnsHours.Text = date_Now.AddHours(CDbl(txtAddHours.Text)).ToString
- txtAnsMinutes.Text = date_Now.AddMinutes(CDbl(txtAddMinutes.Text)).ToString
- TxtAnsSecond. Text = date_Now. AddMinutes (CDbl (txtAddSecond.Text)).ToString
- txtAnsMilliseconds.Text = date_Now.AddMilliseconds(CDbl(txtAddMilliseconds.Text)).ToString
- Catch ex As Exception
- End Try
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'SET THE VALUE IN THE TEXTBOX
- For Each TXT As Control In Me.Controls
- If TXT.GetType Is GetType(TextBox) Then
- TXT.Text = 7
- End If
- Next
- 'CALL THE METHOD TO CALCULATE FOR THE FIRST LOAD
- Calculation_Methods()
- End Sub
click
events of a "Calculate" Button.
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- 'CALL THE METHODS TO CALCULATE EVERYTIME THE BUTTON IS CLICKED
- Calculation_Methods()
- End Sub
Add new comment
- 255 views