vue開發(fā)之moment的介紹與使用
在日常開發(fā)中,我們常常會遇到以下幾種場景:
需要對日期進行非標準格式展示,如 :2021年5月11日星期二下午6點42分需要對日期進行處理,如:要取前24小時的時間 等在這時候用js原生的new Date()處理就有些麻煩了,因此我們找到了moment這個類庫
一、moment是什么?moment 是一個 JavaScript 日期處理類庫。
安裝 moment
如果之前安裝過就不用再安裝了。
npm install moment -- save
注:以下所有時間相對于現(xiàn)在時間:2021/05/11/18:42 星期二
1.日期格式化:
moment().format(’MMMM Do YYYY, h:mm:ss a’); // 五月 11日 2021, 6:42:31 下午moment().format(’dddd’); // 星期二moment().format('MMM Do YY'); // 5月 11日 21moment().format(’YYYY [escaped] YYYY’); // 2021 escaped 2021moment().format(); //2021-05-11T18:06:42+08:00
2.相對時間:
moment('20111031', 'YYYYMMDD').fromNow(); // 2011/10/31號相對于現(xiàn)在是: 10 年前moment('20120620', 'YYYYMMDD').fromNow(); // 2012/06/20號相對于現(xiàn)在是: 9 年前moment().startOf(’day’).fromNow(); //當前日期開始即:2021/05/11/00:00:00相對于現(xiàn)在是: 19 小時前moment().endOf(’day’).fromNow(); //當前日期結(jié)束即:2021/05/11/24:00:00相對于現(xiàn)在是: 5 小時內(nèi)moment().startOf(’hour’).fromNow(); //當前日期小時開始即:2021/05/11/18:00:00相對于現(xiàn)在是: 42分鐘前
3.日歷時間:
moment().subtract(10, ’days’).calendar(); // 當前時間往前推10天的日歷時間: 2021/05/01moment().subtract(6, ’days’).calendar(); // 當前時間往前推6天: 上星期三18:42moment().subtract(3, ’days’).calendar(); // 當前時間往前推3天: 上星期六18:42moment().subtract(1, ’days’).calendar(); // 當前時間往前推1天: 昨天18:42moment().calendar(); // 今天18:42moment().add(1, ’days’).calendar(); // 當前時間往后推1天: 明天18:42moment().add(3, ’days’).calendar(); // 當前時間往后推3天: 下星期五18:42moment().add(10, ’days’).calendar(); // 當前時間往后推10天: 2021/05/21
4.多語言支持:
moment.locale(); // zh-cnmoment().format(’LT’); // 18:42moment().format(’LTS’); // 18:42:31moment().format(’L’); // 2021/05/11moment().format(’l’); // 2021/5/11moment().format(’LL’); // 2021年5月11日moment().format(’ll’); // 2021年5月11日moment().format(’LLL’); // 2021年5月11日下午6點42分moment().format(’lll’); // 2021年5月11日 18:42moment().format(’LLLL’); // 2021年5月11日星期二下午6點42分moment().format(’llll’); // 2021年5月11日星期二 18:42二、使用步驟(例:默認查詢時間24小時之前~當前時間)
1.引入庫
$ npm install moment --save
2.在main.js中全局引入(也可單獨在使用的文件中引入,具體看需求)
import moment from 'moment'Vue.prototype.$moment = moment;
3.在需要使用日期的地方使用
HTML中:
<el-date-picker v-model='timeRange'type='datetimerange'range-separator='至'start-placeholder='開始日期'end-placeholder='結(jié)束日期'> </el-date-picker>
JS中:
data() { return { timeRange:[], } }, mounted(){let start = this.$moment() .subtract(’1’, ’d’) .format(’YYYY-MM-DD HH:mm:ss’) //當前時間往前推1天(24小時):2021-05-10 18:42:53let end = this.$moment().format(’YYYY-MM-DD HH:mm:ss’) //當前時間:2021-05-11 18:42:53this.timeRange=[start,end] }, 三、日期格式格式 含義 舉例 備注 yyyy 年 2021 同YYYY M 月 1 不補0 MM 月 01d 日 2 不補0 dd 日 02dddd 星期 星期二H 小時 3 24小時制;不補0 HH 小時 18 24小時制 h 小時 3 12小時制,須和 A 或 a 使用;不補0 hh 小時 03 12小時制,須和 A 或 a 使用 m 分鐘 4 不補0 mm 分鐘 04s 秒 5 不補0 ss 秒 05A AM/PM AM 僅 format 可用,大寫 a am/pm am 僅 format 可用,小寫
具體方法以及參數(shù)可詳見moment官方文檔
四、new Date() 相關(guān)日期都寫這么多了,那new Date()也一起總結(jié)下吧
let time = new Date(); //獲取當前時間 Tue May 11 2021 18:42:51 GMT+0800 (中國標準時間) let year = time.getFullYear(); //獲取年 2021 let month = time.getMonth() + 1; //獲取月 5 let day = time.getDate(); //獲取天 11 let h = time.getHours(); //獲取小時 18 let m = time.getMinutes(); //獲取分鐘 42 let s = time.getSeconds(); //獲取秒 51 let weekDay = time.getDay(); //獲取星期 2總結(jié)
到此這篇關(guān)于vue開發(fā)之moment的介紹與使用的文章就介紹到這了,更多相關(guān)vue moment使用內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python2.6版本pip安裝步驟解析2. python公司內(nèi)項目對接釘釘審批流程的實現(xiàn)3. python中Ansible模塊的Playbook的具體使用4. Python自動化之定位方法大殺器xpath5. Python本地及虛擬解釋器配置過程解析6. Python 利用flask搭建一個共享服務器的步驟7. 基于python實現(xiàn)matlab filter函數(shù)過程詳解8. Python中Anaconda3 安裝gdal庫的方法9. python自動化測試三部曲之request+django實現(xiàn)接口測試10. Python importlib模塊重載使用方法詳解
