Code for Day & Time
---------------- STEP1 ----------------
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Timer1.Enabled = True
'Timer1.Interval = 1000
'OR
Timer1.Start()
End Sub
---------------- STEP2 ----------------
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox1.Text = UCase(Date.Now.DayOfWeek.ToString()) 'To display current Day of System
Label1.Text = TimeOfDay.ToString("h:mm:ss tt") 'To display current time of System
End Sub
---------------- FOLLOW BELOW STEPS ----------------
NB : First, write code at load event and then come on to the form - Toolbox - double click on Timer - Timer symbol appears at bottom of the form - Double click on it - write Timer1_Tick code as above.Timer is needed for only day and time code not for date.
Code for Date in various format
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = Date.Now.ToLongDateString.ToString '27 November 2022
TextBox1.Text = Date.Now.ToLongTimeString.ToString '08:47:23 AM
-----------------------------------
TextBox1.Text = DateTime.Now.ToString() '27-11-2022 08:55:35 AM
TextBox1.Text = Date.Now.ToString() '27-11-2022 08:57:30 AM
-----------------------------------
Dim dt As DateTime = Now
MsgBox(dt.ToLongDateString) '27 November 2022
TextBox1.Text =dt.ToLongDateString '27 November 2022
TextBox1.Text =dt.ToShortDateString '27-11-2022
TextBox1.Text =dt.ToString '27-11-2022 09:09:10 AM
TextBox1.Text =dt.ToString("yyyy-MM-dd") '2022-11-27
TextBox1.Text =dt.ToString("dd-MM-yyyy") '27-11-2022
TextBox1.Text =dt.ToString("dd-MMM-yyyy") '27-Nov-2022
-----------------------------------
Dim Str As String = Format(DateTimePicker1.Value, "dd-MMM-yyyy")
TextBox1.Text = Str.ToString '27-Nov-2022
MsgBox(Str) '27-Nov-2022
-----------------------------------
Dim dt As DateTime = System.DateTime.Now
TextBox1.Text = dt.ToString() '27-11-2022 09:29:43 AM
-----------------------------------
TextBox1.Text = Date.Today.Year '2022
TextBox1.Text = Year(Now) '2022
-----------------------------------
Dim dt As DateTime = #10/11/2013#
Dim yr As Int32 = dt.Year
TextBox1.Text = yr.ToString() '2013
-----------------------------------
Dim yr As Integer
yr = Convert.ToInt32(Now.ToString("yyyy"))
'yr = Convert.ToInt32(Now.ToString("yy"))
'MsgBox(yr) '2022
TextBox1.Text = yr.ToString '2022
-----------------------------------
Dim yr As String = Now.Year.ToString()
'MsgBox(yr) '2022
TextBox1.Text = yr.ToString '2022
-----------------------------------
Dim yr As Integer = Date.Now.Year
MsgBox(yr) '2022
TextBox1.Text = yr.ToString '2022
-----------------------------------
End Sub
Code for Date Difference
Example : How to find no. of days between two dates using literal given format of date value.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim diff As TimeSpan
Dim date1 As New DateTime(2021, 7, 25)
Dim date2 As New DateTime(2021, 8, 20)
diff = date2 - date1
TextBox1.Text = diff.TotalDays.ToString
'Console.WriteLine("Total Days between dates are = : {0}", diff.TotalDays)
'MsgBox(diff.TotalDays)
End Sub
Output : 26
----------------- OR -----------------
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim d1 As DateTime = New DateTime(2020, 5, 1)
Dim d2 As DateTime = DateTime.Today()
Dim result = (d2 - d1).TotalDays
MsgBox(result.ToString + " " + "days")
End Sub
Output : 947 days
Example : How to find no. of days between two dates using customized/User defined function.
'Creation of function first(step 1)
Function DateDiffval(ByVal StartDate As DateTime, ByVal EndDate As DateTime) As Integer
Dim diff As Integer
diff = (EndDate - StartDate).TotalDays
Return diff
End Function
--------------------------------------------------
'Creation of Two datetimepicker box, one text box and one button(step 2)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'MsgBox((DateDiffval(DateTimePicker1.Value, DateTimePicker2.Value)) & " " & "day/s")
TextBox1.Text = DateDiffval(DateTimePicker1.Value, DateTimePicker2.Value) & " " & "day/s"
End Sub
Example : How to find no. of days between two dates using VB .Net in-built function(DateDiff()).
[Creation of function first(step 1)]
Function DateDiffval(ByVal StartDate As DateTime, ByVal EndDate As DateTime) As Integer
Dim diff As Integer
diff = DateDiff(DateInterval.Day, StartDate, EndDate)
Return diff
End Function
[Creation of Two datetimepicker box, one text box and one button(step 2)]
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
''MsgBox((DateDiffval(DateTimePicker1.Value, DateTimePicker2.Value)) & " " & "day/s")
TextBox1.Text = DateDiffval(DateTimePicker1.Value, DateTimePicker2.Value) & " " & "day/s"
End Sub
-------------------- OR --------------------
[Create Two datetimepicker box, one text box and one button first]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim diff As Integer
diff = DateDiff(DateInterval.Day, DateTimePicker1.Value, DateTimePicker2.Value)
TextBox1.Text = diff.ToString + " " + "Day/s"
End Sub
Example : How to find no. of Months between two dates using VB .Net in-built function(DateDiff()).
[Create Two datetimepicker box, one text box and one button first]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim diff As Integer
diff = DateDiff(DateInterval.Month, DateTimePicker1.Value, DateTimePicker2.Value)
TextBox1.Text = diff.ToString + " " + "Month/s"
End Sub
Example : How to find no. of Years between two dates using VB .Net in-built function(DateDiff()).
[Create Two datetimepicker box, one text box and one button first]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim diff As Integer
diff = DateDiff(DateInterval.Year, DateTimePicker1.Value, DateTimePicker2.Value)
TextBox1.Text = diff.ToString + " " + "Year/s"
End Sub
Example : How to find no. of Hours between two dates using VB .Net in-built function(DateDiff()).
[Create Two datetimepicker box, one text box and one button first]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim diff As Integer
diff = DateDiff(DateInterval.Hour, DateTimePicker1.Value, DateTimePicker2.Value)
TextBox1.Text = diff.ToString + " " + "Hour/s"
End Sub
Example : How to find no. of Minutes between two dates using VB .Net in-built function(DateDiff()).
[Create Two datetimepicker box, one text box and one button first]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim diff As Integer
diff = DateDiff(DateInterval.Minute, DateTimePicker1.Value, DateTimePicker2.Value)
TextBox1.Text = diff.ToString + " " + "Minute/s"
End Sub
Example : How to find Age in the form of Years, Months and Days from Today.
[Create a DateTimePicker box and Button on the form first]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dob As DateTime
dob = New DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day)
Dim tday As TimeSpan = DateTime.Now.Subtract(dob)
Dim years As Integer, months As Integer, days As Integer
months = 12 * (DateTime.Now.Year - dob.Year) + (DateTime.Now.Month - dob.Month)
If DateTime.Now.Day < dob.Day Then
months -= 1
days = DateTime.DaysInMonth(dob.Year, dob.Month) - dob.Day + DateTime.Now.Day
Else
days = DateTime.Now.Day - dob.Day
End If
years = Math.Floor(months / 12)
months -= years * 12
MsgBox("Your age are from today, " & Format(Now, "dd-MMM-yyyy") & " is" & vbCrLf & years & " Years, " & months & " Months and " & days & " Days")
End Sub
0 Comments