开发者

How to convert Date format

I have "Friday, April 02, 2010" as date now I wan开发者_如何学运维t 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?


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.


<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜