插件 ------ momentJs - 在JavaScript中解析,验证,操作和显示日期和时间。

momentJs网址 :https://momentjs.com/

或者使用体积较小的 dayjs : https://github.com/iamkun/dayjs

安装:

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 dayjs --save

 格式日期:

moment().format('MMMM Do YYYY, h:mm:ss a'); // 八月 14日 2019, 2:04:23 下午
moment().format('dddd');                    // 星期三
moment().format("MMM Do YY");               // 8月 14日 19
moment().format('YYYY [escaped] YYYY');     // 2019 escaped 2019
moment().format();                          // 2019-08-14T14:04:23+08:00
                                           

相对时间 :

moment("20111031", "YYYYMMDD").fromNow(); // 8 年前
moment("20120620", "YYYYMMDD").fromNow(); // 7 年前
moment().startOf('day').fromNow();        // 14 小时前
moment().endOf('day').fromNow();          // 10 小时内
moment().startOf('hour').fromNow();       // 5 分钟前

日历时间:

moment().subtract(10, 'days').calendar(); // 2019/08/04
moment().subtract(6, 'days').calendar();  // 上星期四14:06
moment().subtract(3, 'days').calendar();  // 上星期日14:06
moment().subtract(1, 'days').calendar();  // 昨天14:06
moment().calendar();                      // 今天14:06
moment().add(1, 'days').calendar();       // 明天14:06
moment().add(3, 'days').calendar();       // 下星期六14:06
moment().add(10, 'days').calendar();      // 2019/08/24
                                          // undefined

 多语言环境支持:

moment.locale();         // zh-hk
moment().format('LT');   // 14:07
moment().format('LTS');  // 14:07:29
moment().format('L');    // 2019/08/14
moment().format('l');    // 2019/8/14
moment().format('LL');   // 2019年8月14日
moment().format('ll');   // 2019年8月14日
moment().format('LLL');  // 2019年8月14日 14:07
moment().format('lll');  // 2019年8月14日 14:07
moment().format('LLLL'); // 2019年8月14日星期三 14:07
moment().format('llll'); // 2019年8月14日星期三 14:07

dayjs语法:

使用Day.js API可以轻松解析,验证,操作和显示日期和时间。


dayjs('2018-08-08') // parse

dayjs().format('{YYYY} MM-DDTHH:mm:ss SSS [Z] A') // display

dayjs().set('month', 3).month() // get & set

dayjs().add(1, 'year') // manipulate

dayjs().isBefore(dayjs()) // query

国际化

Day.js非常支持国际化。

但除非您使用它们,否则它们都不会包含在您的构建中。

import 'dayjs/locale/es' // load on demand

dayjs.locale('es') // use Spanish locale globally

dayjs('2018-05-05').locale('zh-cn').format()
 // use Chinese Simplified locale in a specific instance

插件(Plugin):

插件是一个独立的模块,可以添加到Day.js以扩展功能或添加新功能。

import advancedFormat from 'dayjs/plugin/advancedFormat' // load on demand

dayjs.extend(advancedFormat) // use plugin

dayjs().format('Q Do k kk X x') // more available formats

 

你可能感兴趣的:(知识点总结,插件)