Getting Current time to display in Label. VB.net - vb.net

I'm trying to build a Break tracker for work and I would like to get a button to display the Current Time in a label. I have tried multiple solutions and this is how far I have gotten.
Sub AddButtonClick(sender As Object, e As EventArgs)
Dim Start as Integer
System.DateTime.Now = Start
total.Text = Start
End Sub
When I do this, I get the error that Property 'Now' is read only.

There are several problems here:
Your assignment is the wrong way round; you're trying to assign a value to DateTime.Now instead of Start
DateTime.Now is a value of type DateTime, not Integer, so the assignment wouldn't work anyway
There's no need to have the Start variable anyway; it's doing no good
total.Text is a property of type String - not DateTime or Integer
(Some of these would only show up at execution time unless you have Option Strict on, which you really should.)
You should use:
total.Text = DateTime.Now.ToString()
... possibly specifying a culture and/or format specifier if you want the result in a particular format.

Try This.....
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label12.Text = TimeOfDay.ToString("h:mm:ss tt")
End Sub

try
total.Text = DateTime.Now.ToString()
or
Dim theDate As DateTime = System.DateTime.Now
total.Text = theDate.ToString()
You declare Start as an Integer, while you are trying to put a DateTime in it, which is not possible.

Use Date.Now instead of DateTime.Now

Related

How to use Datediff of time in vb.net? From Starting time to Ending time

I need to know the value of time. I mean the interval of time; from starting time to ending time. Anyone can help? :(
This is my list and I want to get the interval value of time.
When I select the item/record in the Datagridview2 it will displayed here
Can somebody share an idea/or code? Thanks
To achieve something like this, you need the TimeSpan data type.
In my example, a variable is written with the current time at the beginning of the runtime.
A timer regularly calculates the new time span between time 1 and the current time, and outputs this time span in a text box.
Also, I use CultureInfo (I always do, don't be surprised) and I use PadLeft to have leading zeros. Looks nicer.
Public Class FormMain
Private ReadOnly Starttime As Date = Date.Now
Private ReadOnly Deu As New System.Globalization.CultureInfo("de-DE")
Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim TS As TimeSpan = Date.Now - Starttime
TextBox1.Text = TS.Minutes.ToString(Deu).PadLeft(2, "0"c) & ":" & TS.Seconds.ToString(Deu).PadLeft(2, "0"c)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub
End Class

How to convert 'x' minute format to 'xx' in vb.net

So, I'm making a VB.NET software, and want to show minute format as XX minute, e.g: "05 minute left" and not like this: "5 minute left." My code now like this:
min = DateTime.Now.ToString("mm") + NumericUpDown2.Value
I've tried with big "MM" letters but it's just another time format.
Is that a countdown application, and when I'm try to check the time failed.
If Date.Time.Minutes = min Than
Alarm.show
End If
Although this code has not been tested it should give you some ideas of how to proceed. As far as the formatting is concerned - right pew, wrong church. Number.ToString("00")
Private AlarmTime As DateTime
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AlarmTime = DateTime.Now.AddMinutes(NumericUpDown1.Value)
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim TimeLeft As TimeSpan = AlarmTime - DateTime.Now
Dim MinutesLeft As Integer = TimeLeft.Minutes
Label1.Text = MinutesLeft.ToString("00")
If AlarmTime >= DateTime.Now Then
Alarm.Show()
Timer1.Enabled = False
End If
End Sub
The right way to do this is like this:
DateTime.Now.Minute.ToString("00")

Set start time of Timer Interval

how do i set the starting time of the time interval, let's say i would like to start the 5 sec interval counting after 10 seconds. I'm having a problem with my logic can you help me with this? this is my code:
This is my timer in my Form1
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If DateTime.Now = Form3.DateTimePicker1.Value Then
Timer2.Stop()
MsgBox("hey")
Timer2.Start()
End If
This is the OK button in my Form3
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Form1.Timer2.Interval = New TimeSpan(CInt(NumericUpDown3.Value), CInt(NumericUpDown2.Value), CInt(NumericUpDown1.Value)).TotalMilliseconds
Form1.Timer2.Start()
End If
It seems to work successfully with the intervals without setting the start of the interval, But if i add the start time of the interval, it doesn't seem to work. with this code If DateTime.Now = Form3.DateTimePicker1.Value Then what could be wrong?
This seems to be bad idea:
If DateTime.Now = Form3.DateTimePicker1.Value Then
Because it is extremely hard for you to catch the time exactly as you specify. Simply change = to >=
If DateTime.Now >= Form3.DateTimePicker1.Value Then
Then when you reach the time or later, you would show your message box.

Showing time on label according to selected time zone from combobox (VB)

I wonder how to display the time in a label according to the selected UTC time zone from the list of a combobox. Thanks in advance. This is what have so far.
Private Sub frmClock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label2.Text = TimeOfDay
Dim tzCollection As ReadOnlyCollection(Of TimeZoneInfo)
tzCollection = TimeZoneInfo.GetSystemTimeZones()
Me.ComboBox1.DataSource = tzCollection
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selectedTimeZone As TimeZoneInfo = DirectCast(Me.ComboBox1.SelectedValue(), TimeZoneInfo)
MsgBox("You selected the " & selectedTimeZone.ToString() & " time zone.")
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim dt As Date = TimeOfDay
Label2.Text = dt.ToString()
End Sub
The TimeZoneInfo class provides several shared methods which can be used to convert times from one time-zone to another. For instance, if the time from which you want to convert is UTC, you can use the ConvertTimeFromUtc method, like this:
Dim convertedTime As Date = TimeZoneInfo.ConvertTimeFromUtc(Date.UtcNow, selectedTimeZone)
Or, if the time to convert is in the current local time-zone, then you can use the ConvertTime method, like this:
Dim convertedTime As Date = TimeZoneInfo.ConvertTime(Date.Now, selectedTimeZone)
Alternatively, if the source time is in another time-zone (not UTC nor local), you can use another overload of the ConvertTime method which takes both the source time-zone and the destination time-zone, like this:
Dim convertedTime As Date = TimeZoneInfo.ConvertTime(sourceTime, sourceTimeZoneInfo, destinationTimeZoneInfo)

Multi-Column Combobox And Date Data Type

I am trying to make a program that calculates for how long someone is awake. The user just needs to input the time he wakes up. But I have come across 2 problems:
I need to make a combobox for the user to choose between PM and AM but I don't know how to make a combobox with multiple columns.
How can I make calculations with Date type variables? Because I need to get the current time and subtract by the time the user woke up.
Here's the code for now:
Public Class Form1
Dim wokeup As Date
Dim now As Date
Dim awake As Date
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
now = DateAndTime.TimeOfDay
ComboBox1.Text = "AM"
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
wokeup = TextBox1.Text
Label2.Text = wokeup
End Sub
End Class
1) I'm not sure what you mean by "Multiple Columns" but I am guessing that you just need the user to choose between AM and PM.
Combobox1.Items.Add("AM")
Combobox1.Items.Add("PM")
Combobox1DropDownStyle = ComboBoxStyle.DropDownList
2) To subtract time intervals you can use the following:
Dim timeAwake as TimeSpan = Date.Now.Subtract(CDate(Textbox1.Text))
You may need to add the current date in front of Textbox1.Text if the textbox only contains a time. From timeAwake, you can get the total hours awake using timeAwake.TotalHours.