Using time in VB

degam

Newcomer
Joined
Aug 1, 2008
Messages
3
I am trying to create a program that allows you to enter any time of the day you want. Then I am trying to edit that time, either by adding or subtracting hours, minutes, seconds, or milliseconds. I started on it by every time I run the program I get an error, "When casting from a number, the value must be a number less than infinity." and error details "{"Conversion from string "2" to type 'Date' is not valid."}"

If someone can give me a different way to approach it or any ideas that would be great. Thank you.
 
I'm not sure what you are trying to do -- this may work for you.

Visual Basic:
' you might want to use TryParse for production to eliminate errors
DateTime myDT = DateTime.Parse(TextBox1.Text)
myDT.AddDays(1)
TextBox1.Text = myDT.ToString()

[edit]
PD is right, if you post your troublesome code, it is alot easier for us to help you.
 
Last edited:
Could you post the code you have so far? It is a lot easier for people to offer help if they have something to go on.

As a pointer though .Net provides the Date and TimeSpan classes that allow you to work with dates and durations.
 
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click '

Dim myDt As DateTime
DateTime(myDt = DateTime.Parse(TextBox1.Text))
myDt.AddDays(1)
TextBox1.Text = myDt.ToString()

'DateTime gives me an error: "DateTime is a type cannot be used as an expression"

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txthr.Text = TimeOfDay.Hour
txtMin.Text = TimeOfDay.Minute
txtSec.Text = TimeOfDay.Second
txtMilli.Text = TimeOfDay.Millisecond
End Sub
End Class


Do not have much done but got errors from the start, havnt worked with VB in years and never worked Date and time.
 
Visual Basic:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click '

' this code lets you add one day to a date/time object
Dim myDt As DateTime
' I changed this line
myDt = DateTime.Parse(TextBox1.Text)
myDt.AddDays(1)
TextBox1.Text = myDt.ToString()

'DateTime gives me an error: "DateTime is a type cannot be used as an expression"

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txthr.Text = TimeOfDay.Hour
txtMin.Text = TimeOfDay.Minute
txtSec.Text = TimeOfDay.Second
txtMilli.Text = TimeOfDay.Millisecond
End Sub
End Class

This works because the DateTime class has a static (VB Shared) method called "Parse" which returns a DateTime class itself.
 
Well, I do not see the date changing. Also I wanted to be able to change the time as stated in my question at the very top. Thank you.
 
Back
Top