I will assume that the HTML field is a HTML5 date type field. Depending of the browser and language locale the front end user format could be seen differently, but the underlying value is always defined by the format yyyy-mm-dd
. Also, when a form containing a date field is posted, the server side will receive also this format.
Dates on Javascript are based on the object Date
. But this, additional of the Calendar date, also includes time, microseconds and timezone. To get the current user system date browser formatted on the user locale, can be done using.
var dateLocale= new Date().toLocaleString();
If the date input by the user, using a date input field, getting just the value will give the format 'yyyy-mm-dd'. However there is a way to retrieve the content using the method valueAsDate
. This will return a Date
object. However this Date object will have the time at 12:00 AM at GMT timezone. When you try to retrieve the locale date part, the content could be wrong because it's on the wrong timezone. To workaround the issue add the timezone offset minutes to the Date object to get you the correct locale time and date.
var dv=document.getElementById('date_field').valueAsDate;
dv.setMinutes(dv.getTimezoneOffset());
var datelocale=dv.toLocaleDateString;
Now you can do whatever you want with datelocale
variable, but not try to put it back to a date input field as it could be incompatible. You could put it into a text input type or a hidden type.