我了解DateTime
和AddDays()
命令。但是我需要在我的txtDateTime.Text文本框中输入日期,并在我的txtNights.Text中花费一些时间。仅仅因为我不知道如何将它们加在一起并在第三个texbox中显示它们,这里是我的所有代码......
public partial class Request : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtDateTime.Text = DateTime.Now.ToString("d");
if (!IsPostBack)
{
Calendar1.Visible = false;
}
txtDateTime.Focus();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void imgCalendar_Click(object sender, ImageClickEventArgs e)
{
if (Calendar1.Visible)
{
Calendar1.Visible = false;
}
else
{
Calendar1.Visible = true;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
txtDateTime.Text = Calendar1.SelectedDate.ToShortDateString();
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblThank.Text = "Thank you for your request";
double dblNights = 0;
//Validation
//Validation of Nights
try
{
dblNights = Convert.ToDouble(txtNights.Text);
if (double.TryParse(txtNights.Text, out dblNights))
{
}
else
{
string script = "alert(\"Number of Nights Must be between 1 and 14!\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
txtNights.Focus();
}
}//End Try
catch
{
string script = "alert(\"Number of Nights Must be an Integer!\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
txtNights.Focus();
}//End Catch
//End Nights Validation
//validation of Email/Name Fields
Boolean blnErrors = false;
if (txtName.Text == "")
{
string script = "alert(\"Name Field Is Required!\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
txtName.Focus();
}
if (txtEmail.Text == "")
{
string script = "alert(\"Email Field Is Required!\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
txtEmail.Focus();
}
//End Validation of Email/Name Fields
//End ALL Validation
//Depparture Date
DateTime arrivalDate = Calendar1.SelectedDate;
DateTime departureDate = arrivalDate.AddDays(1);
string formattedDate = departureDate.ToString("dd/MM/yy");
formattedDate = txtDeparture.Text;
}//End Submit
protected void btnClear_Click(object sender, EventArgs e)
{
txtDateTime.Text = "";
txtEmail.Text = "";
txtName.Text = "";
txtNights.Text = "";
txtSpecial.Text = "";
radKing.Checked = false;
radStandard.Checked = false;
radSuite.Checked = false;
radBusiness.Checked = false;
radDouble.Checked = false;
}
protected void txtDeparture_TextChanged(object sender, EventArgs e)
{
DateTime arrivalDate = Calendar1.SelectedDate;
DateTime departureDate = arrivalDate.AddDays(1);
string formattedDate = departureDate.ToString("dd/MM/yy");
formattedDate = txtDeparture.Text;
}
}
我需要将txtDateTime.Text
中选择的日期与我txtNights.Text
中输入的住宿天数进行比较,然后将这两个日期添加到第三个名为{{1}的框中}
如何使用txtDeparture.Text
和DateTime
?
答案 0 :(得分:3)
我有点困惑,但我认为这就是你要找的东西?
DateTime startDate = DateTime.Parse(txtDateTime.Text);
int daysToSpend = int.Parse(txtNights.Text);
DateTime endDate = startDate.AddDays(daysToSpend);
答案 1 :(得分:0)
您需要修改\\Departure Date
方法
btnSubmit_Click(
部分
目前您已编码添加1天。您需要更改以下行。
DateTime arrivalDate = Calendar1.SelectedDate;
DateTime departureDate = arrivalDate.AddDays(1);
string formattedDate = departureDate.ToString("dd/MM/yy");
formattedDate = txtDeparture.Text;
以下
DateTime arrivalDate = Convert.ToDateTime(txtDateTime.Text);
DateTime departureDate = arrivalDate.AddDays(Convert.ToInt32(txtNights.Text));
string formattedDate = departureDate.ToString("dd/MM/yy");
txtDeparture.Text = formattedDate;
或者用更少的代码来做。
DateTime arrivalDate = Convert.ToDateTime(txtDateTime.Text);
txtDeparture.Text = arrivalDate.AddDays(Convert.ToInt32(txtNights.Text)).ToString("dd/MM/yy");