0

I have this code in the asp.net page that works assigning the value to dtStart, but how do I update the variable dtStart to what the user manually enters or modifies the datetime value in the txtStart control box? thanks.

protected void btnStart_Click(object sender, EventArgs e)
    {
        txtStart.Text = DateTime.Now.ToString();
        dtStart = DateTime.Now;
    }
ikask
  • 318
  • 2
  • 11
  • 23

2 Answers2

0

If txtStart.Text contains text which should represent a DateTime then you can try to convert it to exactly that:

DateTime dt;
if (DateTime.TryParse(txtStart.Text, out dt))
    dtStart = dt;

If the value could be parsed to a DateTime, it will be set in the dtStart variable. If not, it won't be. (You can add an else clause, or invert the conditional entirely, to handle the condition where the string value couldn't be parsed to a DateTime.)

David
  • 208,112
  • 36
  • 198
  • 279
0

For this purpose you shouldn't use a simple textbox, but some Date picker control or jQuery.

A textbox can be usable only in that case if you force your user the set a properly formatted string as the date in the textbox and after that try to parse the string to DateTime according to David's answer.

Mitulát báti
  • 2,086
  • 5
  • 23
  • 37
  • not familiar withe the date picker control, but this control is useful for capturing datetime values or just dates? I need to capture start datetime and end datetime values and calculate the time lapsed. – ikask Nov 16 '16 at 21:08
  • I'm almost sure that there are a lot of DateTime controls that also capture Time. Or you can use a Date picker and a Time picker controller. That would be 2 then. – Mitulát báti Nov 16 '16 at 22:14