问题
I have "Friday, April 02, 2010" as date now I want to display "04/02/2010" if browser language is selected english and "02.04.2010" if browser language is selected as German. All I want, is to display the date as per the browser format.
Any Idea how this can be done?
回答1:
It gets complicated fairly quickly, your best bet is to find a library to do it.
There's DateJS, for example, which is free and open and supports 150 or so locales, albeit with different files for each, which might be a pain in terms of converting your input format, if your input is always in English. If there's any chance you can change your input to be something internationalized like ISO 8601 (more here and here), that will make localizing that input a lot easier.
回答2:
<html>
<body>
hi
<script>
var lang = navigator.language;
if (!lang) {
lang = navigator.userLanguage;
}
alert (lang);
if (lang.match (/^en/)) {
document.write ("04/02/2010");
} else if (lang.match (/^de/)) {
document.write ("02.04.2010");
} else {
document.write ("I give up");
}
</script>
</body>
</html>
来源:https://stackoverflow.com/questions/2566151/how-to-convert-date-format