How to convert Date format

只谈情不闲聊 提交于 2019-12-11 11:48:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!