通过添加天数确定日期
问题描述:
大家好
我想知道是否有人可以协助解决时间跨度问题.
我有一个带有今天日期(短日期)的文本框,另一个我想要添加天数(整数)的文本框,然后我希望能够计算添加天数后的日期.
任何帮助都将有所帮助,我知道我必须使用时间跨度,但是有没有更简单的方法.
cheers
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
答
使用AddDays.
Use AddDays.
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.
请参见DateTime.Parse,int.Parse和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 [ ^ ]
也许尝试这样的事情:-我知道C#,但是转换为VB应该相当简单;)
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 ;)
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());
}
希望这会有所帮助,
Ed:)
Hope this helps,
Ed :)
添加减法来自DateTime对象 [ ^ ]