-1

The value attribute of input tag only accepts the YYYY-mm-dd format

<input type="date" value="YYYY-mm-dd">

In my app the user birth date is returned in the format: Jan 1, 1999

If I do this nothing happens:

<input type="date" value="Jan 1, 1999">

So how can I display the user birth date if the format I have is different, maybe write a javascript function that returns the correct format but then the problem is how do you stick that function to an attribute

Emilia-tan
  • 35
  • 1
  • 9

2 Answers2

0

You can add Jquery Datepicker,

Jquery Datepicker

In this you can able to set the which type of format you want

Jquery Datepicker format

Gokul
  • 101
  • 1
  • 5
0

You can translate the string you get to a input type=date's value with vanilla Javascript.

Demo:

<label> click to set value
  <input type="text" value="Jan 1, 1999 " onfocus="document.querySelector('#date').valueAsDate = new Date(new Date(this.value).setDate(new Date(this.value).getDate() + 1))" />
</label>
<label>
Input date:
<input id="date" type="date" value="0"> 
</label>

You will happy to read about .valueAsDate here,

And since it get the day before, i fixed it with setting the date to the day after - You can read about it here.

A. Meshu
  • 4,053
  • 2
  • 20
  • 34