Home > OS >  input type date getting date from another location
input type date getting date from another location

Time:05-11

I have an input of type date, on some computers it returns the date in the format dd/MM/yyyy, but in others it returns in the format MM/DD/yyyy, I would like it to return only format dd/MM/yyyy Brazilian pattern

                        <div >
                            <input formControlName="data" type="date" min="{{today}}" 
                            id="inputData"  >
                       </div>

Would there be a way to do this with just HTML or javascript or an angular pipe? I didn't find anything about it, I searched about it and it says that it changes depending on the locale or browser

CodePudding user response:

The default value of date you get from input type="date" is in (yyyy-mm-dd) format you need to change that to your requirement. For reference you can check here

var dateControl = document.querySelector('input[type="date"]');
console.log(dateControl.value); // prints "2022-05-11"
const [year, month, day] = dateControl.value.split('-');

const result = [day, month, year].join('/');
console.log(result); // prints "11/05/2022"
<input type="date" value="2022-05-11">

CodePudding user response:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date [bold face mine]

Note: The displayed date format will differ from the actual value — the displayed date is formatted based on the locale of the user's browser, but the parsed value is always formatted yyyy-mm-dd.

In other words, you can let browser decide formatting since this will be user dependant for international consumption.

  • Related