ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to Create a Method for Calculating the Date and Time in VB.Net

In this tutorial, I’m going to teach you how to calculate the date and time of the present date in VB.Net. This will help you calculate the months, days, hour, minutes, seconds and milliseconds to the present date. Then, the exact time will appear in each fields accurately.

So, let’s begin:
First, open the Visual Basic, create a new Windows Form Application and make the Form just like this.
datetimecalcualatemethodsform
After doing that, go to the solution explorer and click the view code.
datetimecalcualatemethodsform2
In the code view, create a method for the calculation of the months, day and time.

  1. Private Sub CalculatingMethods()
  2.  
  3. 'INITIALIZE THE INSTANCE METHODS OF THE DATETTIME TYPE.
  4. 'DEMONSTRATING THE DateTime.AddMonth, DateTime.AddDays , DateTime.AddHours
  5. ' , DateTime.AddMinutes , DateTime.AddSeconds AND DateTime.AddMilliseconds
  6. Try
  7. Dim date_Now As DateTime = DateAndTime.Now
  8.  
  9. lblOriginalDate.Text = date_Now
  10. txtAns_Month.Text = date_Now.AddMonths(CInt(txtadd_Months.Text)).ToString
  11. txtAns_Days.Text = date_Now.AddDays(CDbl(txtadd_Days.Text)).ToString
  12. txtAns_Hours.Text = date_Now.AddHours(CDbl(txtAdd_Hours.Text)).ToString
  13. txtAns_Minutes.Text = date_Now.AddMinutes(CDbl(txtAdd_Minutes.Text)).ToString
  14. TxtAnsSecond. Text = date_Now. AddMinutes (CDbl (txtAdd_Second.Text)).ToString
  15. txtAns_Milliseconds.Text = date_Now.AddMilliseconds(CDbl(txtAdd_Milliseconds.Text)).ToString
  16.  
  17. Catch ex As Exception
  18. MsgBox(Me.Text, ex.Message)
  19. End Try
  20. End Sub

After creating a method, you have to set the initial value of the TextBox and the method that 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 INITIAL 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. 'SET THE METHOD TO CALCULATE IN THE FIRST LOAD
  9. CalculatingMethods()
  10. End Sub

And now, you have to set the methods that you have created in the click events handler of a “Calculate” Button.

Download the complete source code and run it on you computer.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!