Welcome to Dream.In.Code
Become a VB Expert!

Join 149,478 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,548 people online right now. Registration is fast and FREE... Join Now!




VB: Adding to a date variable?

 
Reply to this topicStart new topic

VB: Adding to a date variable?

Emper0r
26 Apr, 2007 - 02:09 PM
Post #1

D.I.C Head
**

Joined: 7 Dec, 2006
Posts: 59


My Contributions
Hey guys, basically what I have is a date variable in the following format: mm/dd/yyyy and I'm trying to allow the user to add either days, months or years to this date by typing the following into a text box: 5 Days or 10 Months or 20 Years etc etc etc. Basically a number then a space then what they're adding(Days, Months, or Years).

I'm having trouble figuring out how to make it so it reads the number part and just takes that and adds it to a specified field in the date. Could anyone help me on this? Thanks.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: VB: Adding To A Date Variable?
26 Apr, 2007 - 04:53 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
There are methods available to add a specific number of days, months or years to a Data variable. The methods are AddDays, AddMonths, and AddYears.

In the following example the date stored is 4/26/2007, after the AddDays method the new value will be 5/11/2007.
Example:
CODE

        Dim mydate As Date = New Date(2007, 4, 26)
        mydate = mydate.AddDays(15)

User is online!Profile CardPM
+Quote Post

Emper0r
RE: VB: Adding To A Date Variable?
26 Apr, 2007 - 06:38 PM
Post #3

D.I.C Head
**

Joined: 7 Dec, 2006
Posts: 59


My Contributions
QUOTE(jayman9 @ 26 Apr, 2007 - 05:53 PM) *

There are methods available to add a specific number of days, months or years to a Data variable. The methods are AddDays, AddMonths, and AddYears.

In the following example the date stored is 4/26/2007, after the AddDays method the new value will be 5/11/2007.
Example:
CODE

        Dim mydate As Date = New Date(2007, 4, 26)
        mydate = mydate.AddDays(15)



Thank you very much for the reply, but how would I make it so when a user types in "10 Days" for example(without the quotes) it would add 10 days? I want them to type in more than just the number, so how would I get the program to read the number they type in and the type(days, months or years) to determine what date field to add too? Thanks a bunch.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: VB: Adding To A Date Variable?
26 Apr, 2007 - 10:36 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Why do you want the user to have to type it in?

That opens the possibility for too many mistakes if they mistype the word.

As a suggestion, why not put a combobox on your form with Days, Months, and Years. This way the user only needs to type in the value to add to the date and minimizes the possibility of mistakes. It is much easier just to select the parameter from a list.

If you really don't want to go that route, then you will need to take the input as a String and extract the numerical data from it. You can use the IndexOf method to test each character in the string one at a time and checking if it is a number. Then once you find the end of the numbers extract them using the Substring method and convert them to numerical format.
Substring method.
String.IndexOf method.


User is online!Profile CardPM
+Quote Post

Emper0r
RE: VB: Adding To A Date Variable?
27 Apr, 2007 - 10:12 AM
Post #5

D.I.C Head
**

Joined: 7 Dec, 2006
Posts: 59


My Contributions
Well I've given up using one text box to read the amount and the date field type. So now I have two separate text boxes for each one. But I'm having a problem, the first time a user enters an amount of days it works fine. Then if you try to click the button to make it happen again nothing happens, but if you increase from the last number by 1 it just adds 1 to the date not the actual number. I know this is because the integer variable is holding the value still but how can this be when I set it to static and tell it to be set to 0 after the days are added as shown:

CODE
Public Sub AddToDate()
        Static Dim Number As Integer = 0
        Dim DateType As String
        Number = CInt(txtTimeToAdd.Text)
        DateType = txtDateType.Text

        If DateType.Equals("Days") Then
            txtDate2.Text = date2.AddDays(Number)
            Number = 0
        End If

    End Sub


Any help is greatly appreciated!
User is offlineProfile CardPM
+Quote Post

Emper0r
RE: VB: Adding To A Date Variable?
27 Apr, 2007 - 10:56 AM
Post #6

D.I.C Head
**

Joined: 7 Dec, 2006
Posts: 59


My Contributions
I've decided to give the one textbox route another shot. However, now when I try to run the method I get the following error message:

"Cast from string "5 days" to type 'Integer' is not valid." (the 5 part is just a number I entered)

Please let me know if you see what's wrong in the code:

Here's the code for the new function:

CODE

Public Sub AddToDate()
        Dim Number As Integer
        Dim DateType As String
        DateType = txtDateType.Text
        Number = CInt(DateType.TrimEnd(DateType.Substring(0, DateType.IndexOf(" "))))
        DateType = DateType.TrimStart(DateType.TrimEnd(DateType.Substring(DateType.IndexOf(" "), 6)))

        If DateType.Equals("days") Then
            txtDate2.Text = date2.AddDays(Number)
        End If

        If DateType.Equals("months") Then
            txtDate2.Text = date2.AddMonths(Number)
        End If

        If DateType.Equals("years") Then
            txtDate2.Text = date2.AddYears(Number)
        End If

    End Sub

User is offlineProfile CardPM
+Quote Post

Jayman
RE: VB: Adding To A Date Variable?
27 Apr, 2007 - 02:17 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
The problem is that you cannot use TrimStart and TrimEnd in the manner you are trying to and still get the results you need. You've almost got the Substring method correct, although you can get better results with a minor change. You don't even need to use the TrimEnd. And its a good idea to determine the length of the String using the Length method, rather than using a static number like "6". This will avoid problems when your text is less than 6 characters, as in the word "days".

You do need to remove the space from the text, but you need to use TrimStart on a separate line.
CODE

        Number = CInt(DateType.Substring(0, DateType.IndexOf(" ")))
        DateType = DateType.Substring(DateType.IndexOf(" "), DateType.Length - 1)
        DateType = DateType.TrimStart(" ")


The reason you were having problems with as you post previous to your last post. Is that you probably never modified the date. You just display the modified date directly in the TextBox. You are going to run into the same problem here. Do you see how I modified the first IF statement?

You need to actually modify the date stored in the date2 object. Then put the value of date2 into your textbox. Now you can keep clicking your button and it will keep incrementing based on the value in your textbox. Make the same modifications to the other two IF statements.
CODE

        If DateType.Equals("days") Then
            date2 = date2.AddDays(Number)
            txtDate2.Text = date2
        End If

User is online!Profile CardPM
+Quote Post

Emper0r
RE: VB: Adding To A Date Variable?
27 Apr, 2007 - 02:48 PM
Post #8

D.I.C Head
**

Joined: 7 Dec, 2006
Posts: 59


My Contributions
Thank you very much, this works except when I try to type in more than one digit for the amount to add. 1-9 work great, but 10+ I get the following error:

Index and length must refer to a location within the string.
Parameter name: length.

Is this because of this part of the code: DateType.Length - 1
?

Here's the full method again for reference, thank you very much again for the help:

CODE

Public Sub AddToDate()
        Dim Number As Integer
        Dim DateType As String
        DateType = txtDateType.Text
        Number = CInt(DateType.Substring(0, DateType.IndexOf(" ")))
        DateType = DateType.Substring(DateType.IndexOf(" "), DateType.Length - 1)
        DateType = DateType.TrimStart(" ")

        If DateType.Equals("days") Then
            date2 = date2.AddDays(Number)
            txtDate2.Text = date2
        End If

        If DateType.Equals("months") Then
            date2 = date2.AddMonths(Number)
            txtDate2.Text = date2
        End If

        If DateType.Equals("years") Then
            date2 = date2.AddYears(Number)
            txtDate2.Text = date2
        End If

    End Sub

User is offlineProfile CardPM
+Quote Post

Jayman
RE: VB: Adding To A Date Variable?
27 Apr, 2007 - 03:41 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Sorry, actually that is my mistake. sad.gif

Need to subtract the index number of the space from the overall length to determine how many characters to extract. Modify that line of code to the following.
CODE

DateType = DateType.Substring(DateType.IndexOf(" "), DateType.Length - DateType.IndexOf(" "))

User is online!Profile CardPM
+Quote Post

Emper0r
RE: VB: Adding To A Date Variable?
27 Apr, 2007 - 06:30 PM
Post #10

D.I.C Head
**

Joined: 7 Dec, 2006
Posts: 59


My Contributions
Thank you very much, this works great you've been a big help and I really appreciate it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month