I'm using HTML5 input type=date field and because some browsers still don't support this feature, I would like to create error validation message for browsers that display just normal text field instead of date field. This error message should look like
Please enter date in format: ...
But I need to find the correct format, that the browser is set to. Is there any php/js/jQuery way how to find out this?
I'm using HTML5 input type=date field and because some browsers still don't support this feature, I would like to create error validation message for browsers that display just normal text field instead of date field. This error message should look like
Please enter date in format: ...
But I need to find the correct format, that the browser is set to. Is there any php/js/jQuery way how to find out this?
Share Improve this question edited Aug 20, 2021 at 8:07 Liam 29.7k28 gold badges138 silver badges200 bronze badges asked May 31, 2013 at 14:57 feri bacoferi baco 6354 gold badges8 silver badges16 bronze badges 5- Date formats are determined by the OS not the browser. You should dicate what format they should use, because I assume you're saving it to a database, so this saves you from changing the input for your db – Ryan B Commented May 31, 2013 at 15:08
- I think this has been asked before <stackoverflow./questions/10193294/…> – jimmy Commented May 31, 2013 at 15:09
- @jimmy That question is asking about the HTML5 calendar datepicker; this one is asking about international date formatting. – Blazemonger Commented May 31, 2013 at 15:18
-
See Is there any way to change input type="date" format?. It explains that the
value
attribute of the date input, according to the spec, must be inYYYY-MM-DD
format. But the browser can present the date how it wants - usually either in that same format, or in the user's local format. – Rory O'Kane Commented May 31, 2013 at 15:30 - Does this answer your question? How format JavaScript Date with regard to the browser culture? – Liam Commented Aug 20, 2021 at 8:08
3 Answers
Reset to default 10Thanks to @Jose Vega's answer I was able to find very easy way how to do it.
var now=new Date(2013,11,31);
var str=now.toLocaleDateString();
str=str.replace("31","dd");
str=str.replace("12","mm");
str=str.replace("2013","yyyy");
Error message:
"Please enter date in format:" + str
I have used the globalize library to do something similar. I don't know if its functionality has what you are looking for, but I know it does a good job handling different cultures. I use the window.navigator.userLanguage
to determine culture and then feed it to the globalize library to do its thing. I've had success when dealing with currency and numbers.
EDIT: See if this helps:
var now=new Date();
alert(now.toLocaleString());
taken from How format JavaScript Date with regard to the browser culture?
Reference Displaying proper date format depending on culture
Why not to allow users to enter date in any format they like.
E.g. i am tourist in the USA and the browser says me to use format mm/dd/YYYY instead of my native dd.mm.YYYY.
Just get any date entered by users and parse it with Date.parse()