Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted (edited)

I'm not sure what you are trying to do -- this may work for you.

 

' 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.

Edited by Nate Bross

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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.

Posted

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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...