feri baco
feri baco

Reputation: 635

How to detect date format defined by browser?

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?

Upvotes: 8

Views: 20675

Answers (3)

feri baco
feri baco

Reputation: 635

Thanks 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

Upvotes: 10

Jose Vega
Jose Vega

Reputation: 10258

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

Upvotes: 3

claustrofob
claustrofob

Reputation: 4984

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()

Upvotes: -5

Related Questions