Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > Problem in getting time in VB 2008
 

Problem in getting time in VB 2008

I have three controls on my form

two NumericUpDown and one combobox

now i want to get a time from these three

from one NumericUpDownhours, another NumericUpDown from min and from conbo box AM/PM

But i m failed to store this in one variable

How i can do this.

Please help me

Ganesh Narayan  Tuesday, June 03, 2008 5:38 PM

Try this:

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.NumericUpDown1.Value = System.DateTime.Now.Hour

Me.NumericUpDown2.Value = System.DateTime.Now.Minute

End Sub

You don't need AM or Pm cause is already in th long format.

Ale N1  Tuesday, June 03, 2008 6:38 PM

I think that this'll do what you want:

Code Snippet

Public Class Form1

Dim nudHours As NumericUpDown

Dim nudMinutes As NumericUpDown

Dim cboAMPM As ComboBox

Dim lblShowTime As Label

Private Sub Form1_Load(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles MyBase.Load

cboAMPM = New ComboBox

cboAMPM.Location = New Point(12, 81)

cboAMPM.DropDownStyle = ComboBoxStyle.DropDownList

cboAMPM.Items.Add("AM")

cboAMPM.Items.Add("PM")

cboAMPM.SelectedIndex = 0

Me.Controls.Add(cboAMPM)

nudHours = New NumericUpDown

nudHours.Location = New Point(12, 12)

nudHours.Minimum = 1

nudHours.Maximum = 12

nudHours.Value = 1

Me.Controls.Add(nudHours)

nudMinutes = New NumericUpDown

nudMinutes.Location = New Point(12, 45)

nudMinutes.Minimum = 0

nudMinutes.Maximum = 59

nudMinutes.Value = 0

Me.Controls.Add(nudMinutes)

lblShowTime = New Label

lblShowTime.Location = New Point(12, 139)

lblShowTime.Size = New Size(120, 23)

Me.Controls.Add(lblShowTime)

nudHours.TabIndex = 0

nudMinutes.TabIndex = 1

cboAMPM.TabIndex = 2

lblShowTime.TabIndex = 3

AddHandler nudHours.ValueChanged, AddressOf NumericUpDown_ValueChanged

AddHandler nudMinutes.ValueChanged, AddressOf NumericUpDown_ValueChanged

AddHandler cboAMPM.SelectedValueChanged, AddressOf ComboBox_SelectedValueChanged

ShowTime()

End Sub

Private Sub NumericUpDown_ValueChanged(ByVal sender As Object, _

ByVal e As System.EventArgs)

ShowTime()

End Sub

Private Sub ComboBox_SelectedValueChanged(ByVal sender As Object, _

ByVal e As System.EventArgs)

ShowTime()

End Sub

Private Sub ShowTime()

Dim s As String = nudHours.Value.ToString & ":" & _

nudMinutes.Value.ToString & " " & _

cboAMPM.Text

Dim t As DateTime = Convert.ToDateTime(s)

'Format the time to your liking here!!!

lblShowTime.Text = t.ToString

End Sub

End Class

SMD-  Wednesday, June 04, 2008 1:27 PM

Place the following controls on your form:

two numeric up-down controls named nudHour and nudMinute

one checkbox named chkAMPM

one button named btnOK

one label named lblTime

You don't need a combobox. A single checkbox can be used to switch between AM and PM. Set the text of the checkbox to AM. The CheckedChanged event can switch the text back and forth as the user clicks the checkbox.

Set the minimum and maximum values of nudHour to 1 and 12; of nudMinute to 0 and 59.

The btnOK code is used to copy the selected values to a string and display it in the lblTime label. Here is the code:

Code Snippet

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

Dim hr, min, mer, totime As String

If chkAMPM.Checked = True Then

mer = " PM"

Else

mer = " AM"

End If

hr = nudHour.Value.ToString

min = nudMinute.Value.ToString

If nudMinute.Value < 10 Then

min = "0" & min

End If

totime = hr & ":" & min & mer

lblTime.Text = totime

End Sub

Private Sub chkAMPM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAMPM.CheckedChanged

If chkAMPM.Checked = True Then

chkAMPM.Text = "PM"

Else

chkAMPM.Text = "AM"

End If

End Sub

Solitaire  Wednesday, June 04, 2008 8:40 PM

Use the ToShortTimeString format.

Add the highlightedlines to the code I provided above:

Code Snippet

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

Dim hr, min, mer, totime As String

Dim mytime As Date, time12, time24 As String

If chkAMPM.Checked = True Then

mer = " PM"

Else

mer = " AM"

End If

hr = nudHour.Value.ToString

min = nudMinute.Value.ToString

If nudMinute.Value < 10 Then

min = "0" & min

End If

totime = hr & ":" & min & mer

lblTime.Text = totime

mytime = Convert.ToDateTime(totime)

time12 = mytime.ToShortTimeString

time24 = mytime.TimeOfDay.ToString

MessageBox.Show(time12 & vbCrLf & time24)

End Sub

Solitaire  Saturday, June 07, 2008 3:04 AM

Try this:

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.NumericUpDown1.Value = System.DateTime.Now.Hour

Me.NumericUpDown2.Value = System.DateTime.Now.Minute

End Sub

You don't need AM or Pm cause is already in th long format.

Ale N1  Tuesday, June 03, 2008 6:38 PM

I think that this'll do what you want:

Code Snippet

Public Class Form1

Dim nudHours As NumericUpDown

Dim nudMinutes As NumericUpDown

Dim cboAMPM As ComboBox

Dim lblShowTime As Label

Private Sub Form1_Load(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles MyBase.Load

cboAMPM = New ComboBox

cboAMPM.Location = New Point(12, 81)

cboAMPM.DropDownStyle = ComboBoxStyle.DropDownList

cboAMPM.Items.Add("AM")

cboAMPM.Items.Add("PM")

cboAMPM.SelectedIndex = 0

Me.Controls.Add(cboAMPM)

nudHours = New NumericUpDown

nudHours.Location = New Point(12, 12)

nudHours.Minimum = 1

nudHours.Maximum = 12

nudHours.Value = 1

Me.Controls.Add(nudHours)

nudMinutes = New NumericUpDown

nudMinutes.Location = New Point(12, 45)

nudMinutes.Minimum = 0

nudMinutes.Maximum = 59

nudMinutes.Value = 0

Me.Controls.Add(nudMinutes)

lblShowTime = New Label

lblShowTime.Location = New Point(12, 139)

lblShowTime.Size = New Size(120, 23)

Me.Controls.Add(lblShowTime)

nudHours.TabIndex = 0

nudMinutes.TabIndex = 1

cboAMPM.TabIndex = 2

lblShowTime.TabIndex = 3

AddHandler nudHours.ValueChanged, AddressOf NumericUpDown_ValueChanged

AddHandler nudMinutes.ValueChanged, AddressOf NumericUpDown_ValueChanged

AddHandler cboAMPM.SelectedValueChanged, AddressOf ComboBox_SelectedValueChanged

ShowTime()

End Sub

Private Sub NumericUpDown_ValueChanged(ByVal sender As Object, _

ByVal e As System.EventArgs)

ShowTime()

End Sub

Private Sub ComboBox_SelectedValueChanged(ByVal sender As Object, _

ByVal e As System.EventArgs)

ShowTime()

End Sub

Private Sub ShowTime()

Dim s As String = nudHours.Value.ToString & ":" & _

nudMinutes.Value.ToString & " " & _

cboAMPM.Text

Dim t As DateTime = Convert.ToDateTime(s)

'Format the time to your liking here!!!

lblShowTime.Text = t.ToString

End Sub

End Class

SMD-  Wednesday, June 04, 2008 1:27 PM

Place the following controls on your form:

two numeric up-down controls named nudHour and nudMinute

one checkbox named chkAMPM

one button named btnOK

one label named lblTime

You don't need a combobox. A single checkbox can be used to switch between AM and PM. Set the text of the checkbox to AM. The CheckedChanged event can switch the text back and forth as the user clicks the checkbox.

Set the minimum and maximum values of nudHour to 1 and 12; of nudMinute to 0 and 59.

The btnOK code is used to copy the selected values to a string and display it in the lblTime label. Here is the code:

Code Snippet

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

Dim hr, min, mer, totime As String

If chkAMPM.Checked = True Then

mer = " PM"

Else

mer = " AM"

End If

hr = nudHour.Value.ToString

min = nudMinute.Value.ToString

If nudMinute.Value < 10 Then

min = "0" & min

End If

totime = hr & ":" & min & mer

lblTime.Text = totime

End Sub

Private Sub chkAMPM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAMPM.CheckedChanged

If chkAMPM.Checked = True Then

chkAMPM.Text = "PM"

Else

chkAMPM.Text = "AM"

End If

End Sub

Solitaire  Wednesday, June 04, 2008 8:40 PM

i want to compare two"time" with each other..

say one is 24:44:22 PM and one is 10:24:23 PM

means that one is in 24 hours format and one is 12 hours format

how i can do this

is there any property or function to change 24 hours time format into 12 hour format

Ganesh Narayan  Thursday, June 05, 2008 5:19 PM

Use the ToShortTimeString format.

Add the highlightedlines to the code I provided above:

Code Snippet

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

Dim hr, min, mer, totime As String

Dim mytime As Date, time12, time24 As String

If chkAMPM.Checked = True Then

mer = " PM"

Else

mer = " AM"

End If

hr = nudHour.Value.ToString

min = nudMinute.Value.ToString

If nudMinute.Value < 10 Then

min = "0" & min

End If

totime = hr & ":" & min & mer

lblTime.Text = totime

mytime = Convert.ToDateTime(totime)

time12 = mytime.ToShortTimeString

time24 = mytime.TimeOfDay.ToString

MessageBox.Show(time12 & vbCrLf & time24)

End Sub

Solitaire  Saturday, June 07, 2008 3:04 AM
hello

I do have a different problem i want to solve also a time calcolating problem

i hab en box that must contain 1000 products a machine creates 4 products a shot
1 shot takes 24,4 seconds time

the first i want to try is 4 textboxes
1 = products in a box
2 = products a shot
3 = number of seconds per shot
4 = here i want to calculate hours an minites that wil take to fill the box

after that i want to go a step further then i want to add textboxes so i can tell its now 14:05 the box also contains 44 products so the programm calculates when this box is full and the newxt 3 boxes after this

can anyone help me to solve this problem?
kwajong  Thursday, October 15, 2009 7:32 AM

You can use google to search for other answers

Custom Search

More Threads

• Minimize to System Tray
• saving text with break?
• Make a simple keylogger....
• Count characters in specified line in RichTextBox
• Using pictureboxes
• Regarding Visual Basic (QUESTIONS)
• sql database data calculation
• SendKeys Help...
• MediaElement (audio / video loop)
• getting the CPU temperature