input type date is not filled in

Asked

Viewed 11,103 times

1

I fill a Datetime field with input type="date" and I can record it in the database normally.

When I retrieve the bank record and try to display it with the same type of input, it shows me the field filled with 01/01/0001. If I look at the source code of the page, I see that the date recovered is there, as recorded in the bank, but in the format dd/MM/yyyy.

I know that the input type=date need to receive the date in format yyyy-MM-dd but I don’t know how to do it. What I’m doing wrong?

  • 1

    What server-side language are you using? PHP, MVC C#?

  • 1

    Transform the date of dd/MM/yyyy for yyyy-MM-dd before setting the value.

  • @Beet I think that’s what the author wants help with ;)

  • 1

    @Jorgeb. He asked what he was doing wrong. Not putting the date in the right format is what he’s doing wrong :D!

  • I think this has to do with the configuration of the browser or the micro itself.

  • I am using MVC4 with c#. The values of the database fields are transferred to the class properties by Model Binder. I don’t know how to change the behavior of the Model Binder so that it formats the date in the format expected by datepicker (yyyy-MM-dd) and I wouldn’t like to do this in the handheld.

Show 1 more comment

3 answers

2

Take the die and turn it into your View:

View

<input type="date" value="@SeuModelo.CampoData.ToString("yyyy-MM-dd")" />

Change 'Seumodelo.Campodata' to the respective of your model, remembering that it should be the type DateTime, you can also test with this:

<input type="date" value="@DateTime.Now.ToString("yyyy-MM-dd")" />
  • It worked, Diego! The only thing is that I’m using @Html.Editorfor(p => p.Datanascimento, new { [email protected]("yyyy-MM-dd") }) and with this approach, it doesn’t work. I’d like to continue using @Html.Editorfor because it implements a lot of other things that are useful. Thanks for the help.

  • Put an Html.Editorfor(p => p.Datanascimento, new { @[email protected]("yyyy-MM-dd") })

  • @pcmoraes put here the line for other people with the same problem use as reference.

  • Hi Diego, the last suggestion didn’t work. I got another way to do this with Dataannotations. In the person class, I decorate the Databirth property with [Datatype(Datatype.Date)] and [Displayformat(Dataformatstring = "{0:yyy-MM-dd}", Applyformatineditmode = true)]. Then I can use Editorfor as normal as it works. (I still can’t format my comments like you and I don’t know how to start. Well, that’s another matter).

  • This is not yet the best way to do this, because I need to make this annotation in all the Datetime-like fields of the domain classes. I’ve found code solutions to do this by customizing the MVC Model Binder, but I can’t make it work.

  • Yes, I had seen the solution yesterday, but since I don’t use @Editorfor I was a bit out of options, but this is the way to do it, it’s correct.

Show 1 more comment

0

To those who help I use as follows

    <input type="date" class="form-control" name="dia" id="dia" value="<?php echo date('Y-m-d',strtotime($cli["dia"]));?>" />

Returns the value saved in the database

0

Try it like this:

Search the data in the database, via SQL as you usually do, in this example placed in this variable:

=> $dadosTrazidosDoBancoNestaArray['dataDoBanco']

And treat:

$dataFormatada = trim($dadosTrazidosDoBancoNestaArray['dataDoBanco']);
$dataFormatada = date("**Y-m-d**", strtotime($dataFormatada));

Print in the field type="date" or type="text" but in the case of type="text" change the date display order to "d-m-Y" to make it right.

<input readonly type="date" name='data' value="<?php echo $dataFormatada ?>">
<input readonly type="text" name='data' value="<?php echo $dataFormatada ?>">

Note that the syntax is practically the same, however, I note that it will not be displayed in the date field with the formatting in "d-m-Y", because it will only recognize the syntax "Y-m-d".

Browser other questions tagged

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