为moment.js正名

说来也奇怪,总有人在耳边说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之类的,同时提供高度可定制化解决方案。至于为啥有人坚决抵制,我只能说,我就静静的坐在这里,等着您提出的任何一个反例吧。

阿里云 OSS(Object Storage Service)是一种云存储服务,而 Moment.js 是一个用于解析、验证、操作和显示日期和时间的 JavaScript 库。这两者之间并没有直接的关联。 Moment.js 是一个开源的 JavaScript 库,您可以在您的项目中使用 Moment.js 来处理日期和时间相关的操作。您可以通过以下步骤在您的项目中使用 Moment.js: 1. 下载 Moment.js:您可以从 Moment.js 的官方网站(https://momentjs.com/)上下载最新版本的 Moment.js 库,或者使用CDN链接直接引入。 2. 引入 Moment.js:将 Moment.js 文件引入到您的项目中。您可以通过在 HTML 文件的头部添加 `<script>` 标签来引入 Moment.js 文件,或者在使用模块化的 JavaScript 环境中使用相应的导入语句。 3. 使用 Moment.js:一旦成功引入 Moment.js,您就可以开始使用它来操作日期和时间。Moment.js 提供了丰富的 API,包括日期解析、格式化、计算、比较等功能。您可以参考 Moment.js 的官方文档和示例以了解如何使用各种功能。 需要注意的是,Moment.js 在2020年之后停止了维护,并且官方推荐使用类似 Day.js 或原生 JavaScript 的日期和时间相关 API。所以,在选择日期和时间处理库时,您也可以考虑其他现代化的替代方案。 至于与阿里云 OSS 的关联,您可以在使用 Moment.js 时,通过获取 OSS 中的对象的相关元数据,如最后修改时间等,然后使用 Moment.js 对日期和时间数据进行格式化和处理。但是,这两者并没有直接的集成或依赖关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值