说来也奇怪,总有人在耳边说moment.js对国际化日期支持不好,坚决不要使用,会带来很多问题之类的话。但就我个人经验来看,还从未见到过任何一个反例,恰好我又是个不信邪的人,并坚持认为检验真理的唯一标准就是小马过河,所以今儿我就准备趟趟水,是骡子是马,咱们拉出来溜溜呗,走起来。
首先,部署方式有如下几种:
npm install moment --save # npm
yarn add moment # Yarn
Install-Package Moment.js # NuGet
spm install moment --save # spm
meteor add momentjs:moment # meteor
bower install moment --save # bower (deprecated)
这里我选择了npm install的方式。随后引入moment-with-locales.min.js,开始编写一段简单的测试代码。
<html>
<head>
</head>
<body>
<script src="moment-with-locales.min.js" charset="UTF-8"></script>
<script type="text/javascript">
//loc is the locale that get from browser
moment.locale(loc);
document.write(moment().format('LT'));
document.write("</br>");
document.write(moment().format('LTS'));
document.write("</br>");
document.write(moment().format('L'));
document.write("</br>");
document.write(moment().format('l'));
document.write("</br>");
document.write(moment().format('LL'));
document.write("</br>");
document.write(moment().format('ll'));
document.write("</br>");
document.write(moment().format('LLL'));
document.write("</br>");
document.write(moment().format('lll'));
document.write("</br>");
document.write(moment().format('LLLL'));
document.write("</br>");
document.write(moment().format('llll'));
</script>
</body>
</html>
打印结果如下。
//fr
11:50
11:50:01
21/07/2018
21/7/2018
21 juillet 2018
21 juil. 2018
21 juillet 2018 11:50
21 juil. 2018 11:50
samedi 21 juillet 2018 11:50
sam. 21 juil. 2018 11:50
//de
11:50
11:50:38
21.07.2018
21.7.2018
21. Juli 2018
21. Juli 2018
21. Juli 2018 11:50
21. Juli 2018 11:50
Samstag, 21. Juli 2018 11:50
Sa., 21. Juli 2018 11:50
//it
11:51
11:51:55
21/07/2018
21/7/2018
21 luglio 2018
21 lug 2018
21 luglio 2018 11:51
21 lug 2018 11:51
sabato 21 luglio 2018 11:51
sab 21 lug 2018 11:51
//zh-cn
11:52
11:52:35
2018/07/21
2018/7/21
2018年7月21日
2018年7月21日
2018年7月21日中午11点52分
2018年7月21日 11:52
2018年7月21日星期六中午11点52分
2018年7月21日星期六 11:52
//zh-tw
11:53
11:53:52
2018/07/21
2018/7/21
2018年7月21日
2018年7月21日
2018年7月21日 11:53
2018年7月21日 11:53
2018年7月21日星期六 11:53
2018年7月21日星期六 11:53
//ko-kr
오전 11:54
오전 11:54:25
2018.07.21.
2018.07.21.
2018년 7월 21일
2018년 7월 21일
2018년 7월 21일 오전 11:54
2018년 7월 21일 오전 11:54
2018년 7월 21일 토요일 오전 11:54
2018년 7월 21일 토요일 오전 11:54
//ja-jp
11:54
11:54:51
2018/07/21
2018/07/21
2018年7月21日
2018年7月21日
2018年7月21日 11:54
2018年7月21日 11:54
2018年7月21日 土曜日 11:54
2018年7月21日(土) 11:54
//es
11:55
11:55:20
21/07/2018
21/7/2018
21 de julio de 2018
21 de jul. de 2018
21 de julio de 2018 11:55
21 de jul. de 2018 11:55
sábado, 21 de julio de 2018 11:55
sáb., 21 de jul. de 2018 11:55
//ar
١١:٥٥
١١:٥٥:٤٠
٢١/٧/٢٠١٨
٢١/٧/٢٠١٨
٢١ يوليو ٢٠١٨
٢١ يوليو ٢٠١٨
٢١ يوليو ٢٠١٨ ١١:٥٥
٢١ يوليو ٢٠١٨ ١١:٥٥
السبت ٢١ يوليو ٢٠١٨ ١١:٥٥
سبت ٢١ يوليو ٢٠١٨ ١١:٥٥
//ru
11:56
11:56:54
21.07.2018
21.7.2018
21 июля 2018 г.
21 июля 2018 г.
21 июля 2018 г., 11:56
21 июля 2018 г., 11:56
суббота, 21 июля 2018 г., 11:56
сб, 21 июля 2018 г., 11:56
//pt-br
11:58
11:58:12
21/07/2018
21/7/2018
21 de julho de 2018
21 de jul de 2018
21 de julho de 2018 às 11:58
21 de jul de 2018 às 11:58
Sábado, 21 de julho de 2018 às 11:58
Sáb, 21 de jul de 2018 às 11:58
//pt
11:58
11:58:52
21/07/2018
21/7/2018
21 de julho de 2018
21 de jul de 2018
21 de julho de 2018 11:58
21 de jul de 2018 11:58
Sábado, 21 de julho de 2018 11:58
Sáb, 21 de jul de 2018 11:58
相信大家也都看到了,so far so good, isn’t it? 另外moment还有很多的使用办法和技巧,例如fromNow、list of locales之类的,同时提供高度可定制化解决方案。至于为啥有人坚决抵制,我只能说,我就静静的坐在这里,等着您提出的任何一个反例吧。