degam Posted August 1, 2008 Posted August 1, 2008 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. Quote
Nate Bross Posted August 1, 2008 Posted August 1, 2008 (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 August 1, 2008 by Nate Bross Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Administrators PlausiblyDamp Posted August 1, 2008 Administrators Posted August 1, 2008 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
degam Posted August 1, 2008 Author Posted August 1, 2008 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. Quote
Nate Bross Posted August 1, 2008 Posted August 1, 2008 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
degam Posted August 1, 2008 Author Posted August 1, 2008 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. Quote
Nate Bross Posted August 1, 2008 Posted August 1, 2008 You can use the DateTime objects .Add(TimeSpan) method You could use one of the other .Add[unit] methods: Dim dt as new DateTime() dt.AddDays(1) dt.AddMinutes(1440) dt.AddHours(24) Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.