我正在制作一个项目,其中我必须计算从开始日期到结束日期的总天数,用户在运行时在asp.net c#.i中插入文本框,必须在button_click event.how上执行此操作去做这个? 我试过这个 -
protected void TextBox14_TextChanged(object sender, EventArgs e)
{
// get date from first text box
DateTime dold = Convert.ToDateTime(TextBox1.Text);
DateTime dnew = Convert.ToDateTime(TextBox14.Text);
TimeSpan daydif = (dnew - dold);
double dayd = daydif.TotalDays;
Label27.Text = dayd.ToString();
}
答案 0 :(得分:1)
试试这个:
int diffDays = (date1.Date - date2.Date).Days;
希望这可以帮到你!
答案 1 :(得分:0)
尝试了两个文本框更改
private void frm_TextChanged(object sender,TextChangedEventArgs e) { calculatedays(); }
private void to_TextChanged(object sender, TextChangedEventArgs e)
{
calculatedays();
}
public void calculatedays()
{
if (frm.Text == string.Empty) return;
DateTime dt;
DateTime.TryParse(frm.Text, out dt);
if (frm.Text == string.Empty) return;
DateTime dt2;
DateTime.TryParse(frm.Text, out dt2);
if (dt == DateTime.MinValue && dt2 == DateTime.MinValue) return;
TimeSpan daydif = (dt2 - dt);
double dayd = daydif.TotalDays;
string s = dayd.ToString();
}