Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi All

I was wondering if someone could possibly assist in a timespan issue.

I have a textbox with todays date (shortdate) and another textbox that i want to beable to add days (integer) and then i want to be able to work what the date will be after adding the number of days.

Any Help would be helpful, i know i have to use timespan, but is there an easier way.

cheers
Posted

Use AddDays.

VB
Dim today As System.DateTime
Dim future As System.DateTime

today = System.DateTime.Now
future = today.AddDays(NumberOfDays)

MessageBox.Show(future)

That should do it.

You just have to provide the number of days.
 
Share this answer
 
Comments
SIFNOk 18-May-11 17:24pm    
Thanks For The Reply Dave!

Yeah that seems to work....what if i had a textbox with a shortdate thats a string, how could i get the same result from that ?
Dalek Dave 18-May-11 17:29pm    
http://www.codeproject.com/KB/cs/String2DateTime.aspx
Sergey Alexandrovich Kryukov 18-May-11 17:33pm    
Correct, a 5.
--SA
SIFNOk 18-May-11 17:41pm    
Cheeers Davee! Your Absolute Legend! *Puts Dave on wall of legends!

Works Dope!
Edit: See DateTime.Parse, int.Parse and DateTime.AddDays:
DateTime.AddDays: http://msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx[^]

DateTime.Parse: http://msdn.microsoft.com/en-us/library/1k1skd40.aspx[^]

int.Parse: http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx[^]

Perhaps try something like this: - I know C# but it should be fairly simple to convert to VB ;)

C#
TextBox DateBox;
TextBox DaysBox;

DateTime AddDays(string DateString, string DaysString)
{
    DateTime TheDate = DateTime.Parse(DateString);
    int Days = int.Parse(DaysString);
    
    return TheDate.AddDays((double)Days);
}

//Event method called by both DateBox text changed event and DaysBox text changed event.
void TextChanged(object sender, EventArgs e)
{
    DateTime Result = AddDays(DateBox.Text, DaysBox.Text);
    MessageBox.Show(Result.ToString());
}


Hope this helps,

Ed :)
 
Share this answer
 
v2
Comments
SIFNOk 18-May-11 17:26pm    
Thanks Edman!, The References has actualy solved many pondering issues in my project....much appericated. i will give it more of a go later today :)
Ed Nutting 18-May-11 17:27pm    
Good to hear :) Good luck with your project! :)
Sergey Alexandrovich Kryukov 18-May-11 17:37pm    
Down-voted with no apparent reason; and I cannot see any explanation... well, probably I can ignore it then. Maybe, this is because of C# instead of VB.NET. (Right, I usually don't write C# code instead of VB.NET (I hardly can imaging myself installing VB.NET), so I write just in words). I vote 5, because anyone who needs .NET help is supposed to understand at least some C#.
--SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900