vue開發之moment的介紹與使用
在日常開發中,我們常常會遇到以下幾種場景:
需要對日期進行非標準格式展示,如 :2021年5月11日星期二下午6點42分需要對日期進行處理,如:要取前24小時的時間 等在這時候用js原生的new Date()處理就有些麻煩了,因此我們找到了moment這個類庫
一、moment是什么?moment 是一個 JavaScript 日期處理類庫。
安裝 moment
如果之前安裝過就不用再安裝了。
npm install moment -- save
注:以下所有時間相對于現在時間: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號相對于現在是: 10 年前moment('20120620', 'YYYYMMDD').fromNow(); // 2012/06/20號相對于現在是: 9 年前moment().startOf(’day’).fromNow(); //當前日期開始即:2021/05/11/00:00:00相對于現在是: 19 小時前moment().endOf(’day’).fromNow(); //當前日期結束即:2021/05/11/24:00:00相對于現在是: 5 小時內moment().startOf(’hour’).fromNow(); //當前日期小時開始即:2021/05/11/18:00:00相對于現在是: 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='結束日期'> </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 可用,小寫
具體方法以及參數可詳見moment官方文檔
四、new Date() 相關日期都寫這么多了,那new Date()也一起總結下吧
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總結
到此這篇關于vue開發之moment的介紹與使用的文章就介紹到這了,更多相關vue moment使用內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: