Hello I wanted to format the date of my form in the page load, because the date is coming from the database in international format (yyyy-mm-dd).
I made this function to format my date, however I have 2 forms on the same page and both have inputs of the same name. My input is in text format.
function formataData(ele){ var value = $(ele).val(); var d = undefined; if(value.search('-') > -1){ d = $(ele).val().split('-'); console.log(d); } else if($(ele).val().search('/') > -1) { d = $(ele).val().split('/'); console.log(d); } if(d !== undefined){ if(d[0].length > 3){ $(ele).val(d[2]+'/'+d[1]+'/'+d[0]); }else { $(ele).val(d[2]+'-'+d[1]+'-'+d[0]); } } }
Summarizing what I wanted
-The page loads like this:
<input type="text" name="vencimento" value="yyyy-mm-dd" class="form-control">
- I wanted to call my javascript / JQuery function on page load to look like this:
<input type="text" name="vencimento" value="dd-mm-yyyy" class="form-control">
I used $ (document) .ready (function () {}); but it was too changed: / wanted to reuse this function in other codes! Thank you for your attention.
Note: I had forgotten, I want to call this function for all date fields in the page loading too