vue 使用moment獲取當前時間及一月前的時間
其實這個沒啥,就兩行代碼,比較容易忘,主要可以用在按時間段查詢。
安裝 moment
如果之前安裝過就不用再安裝了。
npm install moment -- save
使用 moment
在使用的文件引用 moment。
import moment from 'moment'
然后在需要使用的地方使用就可以了。
let startDate = moment().subtract(30, "days").format('YYYY-MM-DD')
let endDate = moment().format('YYYY-MM-DD').subtract(30, "days") 的意思是向前推30天,如果是1,就是向前推1天。
YYYY-MM-DD:HH:MM:SS 這個是時分秒的格式化。
//獲取當前時間
var now = moment().toDate();//Mon Jul 06 2020 13:50:51 GMT+0800 (中國標準時間)
console.log(now)
//格式化當前時間
now = moment().format('YYYY-MM-DD');//2020-07-06
console.log(now);
//其它幾種格式化方法
now = moment().format('L') // 10/22/2016?
console.log(now);
now = moment().format('LL') // October 22, 2016
console.log(now);
//格式化當前時間
now = moment().format('YYYY-MM-DD:HH:MM:SS');
console.log(now);
//獲取這個月初時間
let startMonth = moment().startOf('month').toDate();
console.log(startMonth);
//獲取今天開始的時間
let dayOfStart = moment().startOf('day').toDate();
console.log(dayOfStart);
//獲取今天結(jié)束的時間
let dayOfEnd = moment().endOf('day').toDate();
console.log(dayOfEnd);
//獲?。玭小時
let lateHour = moment().add(2,'hour').toDate();
console.log(lateHour);
//獲?。玭小時
console.log('//獲取-n小時')
let beforeHour = moment().subtract(2,'hour').toDate();
console.log(beforeHour);
//獲?。玭天
let lateDay = moment().add(+5,'day').toDate();
console.log(lateDay);
//獲取-n天
let beforeDay = moment().add(-5,'day').toDate();
console.log(beforeDay);
//也可以表示為
beforeDay = moment().subtract(5,'day').toDate();
console.log(beforeDay);
console.log('//獲取+n月')
let lateMonth = moment().add(2,'month').toDate();
console.log(lateHour);
//獲?。玭月
let beforeMonth = moment().subtract(2,'month').toDate();
console.log(lateHour);
//獲取星期
let week = moment().format('dddd');
console.log(week);
//獲取到現(xiàn)在的年限 如果不滿一年顯示出具體幾個月
let years = moment('2020-12-31').fromNow();
console.log(years);到此這篇關(guān)于vue 使用moment獲取當前時間及一月前的時間的文章就介紹到這了,更多相關(guān)vue獲取當前日期時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue如何動態(tài)獲取當前時間
- vue怎樣獲取當前時間,并且傳遞給后端(不用注解)
- Vue 中如何利用 new Date() 獲取當前時間
- 基于vue實現(xiàn)8小時帶刻度的時間軸根據(jù)當前時間實時定位功能
- vue中實現(xiàn)當前時間echarts圖表時間軸動態(tài)的數(shù)據(jù)(實例代碼)
- Vue 按照創(chuàng)建時間和當前時間顯示操作(剛剛,幾小時前,幾天前)
- Vue 中獲取當前時間并實時刷新的實現(xiàn)代碼
- Vue filter 過濾當前時間 實現(xiàn)實時更新效果
- vue 2.1.3 實時顯示當前時間,每秒更新的方法
- element-ui vue input輸入框自動獲取焦點聚焦方式
- Vue3新增時自動獲取當前時間的操作方法
相關(guān)文章
Vue項目通過vue-i18n實現(xiàn)國際化方案(推薦)
這篇文章主要介紹了Vue項目通過vue-i18n實現(xiàn)國際化方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
Vue scrollBehavior 滾動行為實現(xiàn)后退頁面顯示在上次瀏覽的位置
這篇文章主要介紹了Vue scrollBehavior 滾動行為實現(xiàn)后退頁面顯示在上次瀏覽的位置,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
vue登錄頁實現(xiàn)使用cookie記住7天密碼功能的方法
這篇文章主要介紹了vue登錄頁實現(xiàn)使用cookie記住7天密碼功能的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02

