如何将日期添加到日期中

本文关键字:日期 添加 | 更新日期: 2023-09-27 17:49:14

我知道DateTimeAddDays()命令。但是我需要在我的txtDateTime中取日期。输入文本框,并在我的txtNights.Text中记录晚上的时间。只是因为我不知道如何将两者加在一起并在第三个文本框中显示它们,这里是我所有的代码…

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中输入住宿的天数,并将这两个加起来并显示在第三个名为txtDeparture.Text的框中

如何处理DateTimeAddDays()呢?

如何将日期添加到日期中

我有点困惑,但我认为这是你在寻找什么?

DateTime startDate = DateTime.Parse(txtDateTime.Text);
int daysToSpend = int.Parse(txtNights.Text);
DateTime endDate = startDate.AddDays(daysToSpend);

需要在btnSubmit_Click(方法中修改''Departure Date section

当前您已编码添加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");