How to set today’s date as default value in a texbox as date input type?

Asked

Viewed 2,873 times

4

I have a research page for a sales report and I use a textbox that generates a input type "date", and whenever the page loads the value of the field is "dd/mm/yyyy".

I would like that whenever the page was loaded the field would show the current date without I need to open the calendar to choose the date. How can I do that?

1 answer

2


From what I understand, you’re wearing a DateTimePicker.

At the event Page_Load, I believe it is only necessary to define the field with the current date.

txtData.Value = DateTime.Today;

EDIT

In the comments, you are having as a result on screen the following:

<input name="calInicio" type="date" id="calInicio" class="form-control" placeholder="DD/MM/AAAA" style="height:32px;width:180px;">

The problem is that <input type="date" ...> must have as initial value a date in the format yyyy-mm-dd (see the part about value). That is, in the Page_Load carry your TextBox as follows:

txtInicio.Text = DateTime.Today.ToString("yyyy-MM-dd");
  • I put this in the programming, but when it loads the page does not work, I put it -> txtInicio.Text = Datetime.Today.Toshortdatestring(); The attribute Value does not have in a textbox

  • Ah, I get it. I’m going to edit the answer. First I want to understand: you do an HTML inspection and the <input> appears with at least one value? Or nothing shows up?

  • Nothing appears

  • The result in HTML by chance is <input type="date" ...>?

  • It looks like this: <input name="calInicio" type="date" id="calInicio" class="form-control" placeholder="DD/MM/YYYY" style="height:32px;width:180px;">

  • Now it all makes sense. Keep an eye out. I’ll edit the answer.

  • 1

    Guy worked out, thank you very much !

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.